์ผ | ์ | ํ | ์ | ๋ชฉ | ๊ธ | ํ |
---|---|---|---|---|---|---|
1 | 2 | |||||
3 | 4 | 5 | 6 | 7 | 8 | 9 |
10 | 11 | 12 | 13 | 14 | 15 | 16 |
17 | 18 | 19 | 20 | 21 | 22 | 23 |
24 | 25 | 26 | 27 | 28 | 29 | 30 |
- ๋๋ฆผ์ฝ๋ฉ
- ํฌ๋กค๋ง
- CSS
- error
- Til
- heapq
- ์ฝ๋ฉํ ์คํธ
- ๊ตญ๋น์ง์
- JS
- ํ๋ก๊ทธ๋๋จธ์ค
- ํ๋ก์ ํธ
- node.js
- HTML
- Python
- ์ฝ๋ฉ
- ์๊ณ ๋ฆฌ์ฆ
- fe
- ๊ฐ๋ฐ
- ์ฝ๋ฉ์ ํ
- ๋ฐฑ์ค
- mongodb
- ํ๋ก ํธ์๋
- javascript
- ๊ทธ๋ฆฌ๋
- ํ ์ดํ๋ก์ ํธ
- ๋ ธ๋ง๋์ฝ๋
- KDT
- react
- ํ์ด์ฌ
- ๋ชจ๊ฐ์ฝ
- Today
- Total
๐ฑ → ๐ณ
[JavaScript] JavaScript ๊ธฐ์ด ๋ณธ๋ฌธ
๐ ๋ฌธ์์ด ๊ด๋ จ JS ๊ธฐ๋ฅ
โ .length();
๋ฌธ์์ด ๊ธธ์ด
โ .indexOf("");
๋ฌธ์์ด ์์น ์ฐพ๊ธฐ
const str = "Hello, world";
console.log(str.indexOf("world"));
// 7
โ .slice(start, end);
๋ฌธ์์ด ์๋ฅด๊ธฐ
โ .replace("์ฐพ์ ๋ฌธ์", "๋ฐ๊ฟ ๋ฌธ์");
๋ฌธ์์ด ๋ฐ๊พธ๊ธฐ
โ .repeat(๋ฐ๋ณต ํ์);
๋ฌธ์์ด ๋ฐ๋ณตํ๊ธฐ
let str = "์ฐ์";
let longStr = str.repeat(5) + "์ฐ";
console.log(longStr);
โ .trim();
์ ๋ค ๊ณต๋ฐฑ ๋ฌธ์ ์ ๊ฑฐํ๊ธฐ
let str = " ์ฐ์์ฐ ";
let trimStr = str.trim();
console.log(trimStr); //์ฐ์์ฐ
๐ ์ซ์ + ์ํ ๊ด๋ จ JS ๊ธฐ๋ฅ
โ int/ float
const pi = 3.14159265358979;
console.log(pi);
let integer = parseInt(pi);
let float = parseFloat(pi);
console.log(integer);
console.log(float);
console.log(typeof integer, typeof float);
์ ์ํ ๋ณํ: parselnt()
๋ถ๋์์ ๋ณํ: parseFloat()
โ ๊ทธ ์ธ ์ํ ํจ์๋ค
let minus = -999;
// Math -> JS ๊ธฐ๋ณธ ๋ด์ฅ ํจ์
console.log("abs : ", Math.abs(minus));
console.log("min : ", Math.min(1, 2, 3, 4, -1));
console.log("max : ", Math.max(1, 2, 3, 4, 77));
console.log("ceil : ", Math.ceil(3.14));
console.log("floor : ", Math.floor(3.99));
console.log("round : ", Math.round(3.14));
console.log("random : ", Math.random());
๐ ๋ฐฐ์ด
โ ์ถ๊ฐ, ์ญ์
// ์ฃผ์, ์๋ณธ ๋ฐฐ์ด์ด ๋ณํจ
- .push(); ๋ง์ง๋ง์ ์ถ๊ฐ
- .unshift(); ์ฒ์์ ์ถ๊ฐ
- .pop(); ๋ง์ง๋ง ์์ ์ญ์ & ๋ฐํ
- .shift(); ์ฒซ ์์ ์ญ์ & ๋ฐํ
- .splice();
// splice
array.splice(start[, deleteCount[, item1[, item2[, ...]]]])
โ ๊ธธ์ด .length;
โ ํฉ์น๊ธฐ .concat();
์๋ณธ ๋ฐฐ์ด ๋ณ๊ฒฝํ์ง ์์
๋ฐํ๊ฐ์ด ์กด์ฌ
let numbers = [1, 2, 3, 4];
let fruits = ["์ฌ๊ณผ", "๋ธ๊ธฐ", "์๋ฐ"];
console.log(numbers.concat(fruits)); //[1, 2, 3, 4, "์ฌ๊ณผ", "๋ธ๊ธฐ", "์๋ฐ"]
console.log(numbers); // [1, 2, 3, 4]
console.log(fruits); // ["์ฌ๊ณผ", "๋ธ๊ธฐ", "์๋ฐ"]
โ for ๋ฌธ
๋ฐฐ์ด์์ ๊ฐ ํ๋์ฉ ์ถ๋ ฅ
โ๏ธ ๊ธฐ๋ณธ for๋ฌธ
let numbers = [1, 2, 3, 4, 5, 6];
let fruits = ["์ฌ๊ณผ", "๋ฐ๋๋", "์๋ฐ", "ํฌ๋", "ํ์ธ์ ํ"];
for (let i = 0; i < numbers.length; i++) {
console.log(numbers[i]);
}
for (let i = 0; i < fruits.length; i++) {
console.log(fruits[i]);
}
โ๏ธ for of๋ฌธ
let numbers = [1, 2, 3, 4, 5, 6];
let fruits = ["์ฌ๊ณผ", "๋ฐ๋๋", "์๋ฐ", "ํฌ๋", "ํ์ธ์ ํ"];
for (let i of numbers) {
console.log(i);
}
for (let i of fruits) {
console.log(i);
}
โ๏ธ forEach๋ฌธ
let numbers = [1, 2, 3, 4, 5, 6];
let fruits = ["์ฌ๊ณผ", "๋ฐ๋๋", "์๋ฐ", "ํฌ๋", "ํ์ธ์ ํ"];
numbers.forEach(function (item, index, array) {
console.log(item, index, array);
});
fruits.forEach(function (item, index, array) {
console.log(item, index, array);
});
โ๏ธ ํ์ดํ ํจ์ ์ฌ์ฉ
numbers.forEach((item, index, array) => {
console.log(item, index, array);
});
โ .map[]
๋ฐ์ดํฐ๋ฅผ ๋ฐํ + ๋ฐฐ์ด๋ก ๋ง๋ค์ด ์ค
let numbers = [1, 2, 3, 4, 5, 6];
let fore = numbers.forEach(function (item) {
return item;
});
let map = numbers.map(function (item) {
return item;
});
// forEach๋ return๊ฐ ๋ฐํํ์ง ์์
console.log(fore); // undefined
console.log(map); // [1, 2, 3, 4, 5, 6]
// ํ์ดํ ํจ์ 1
let map2 = numbers.map(() => {
return item;
});
// ํ์ดํ ํจ์ 2
let map3 = numbers.map((item) => item);
// map ํ์ฉ
const fruits = ["์ฌ๊ณผ", "ํ์ธ์ ํ", "์๋ฐ", "ํฌ๋", "์ค๋ ์ง"];
let obj = fruits.map((item, index) => {
return {
id: index + 1,
name: item,
};
});
console.log(obj);
โ [].reduce
๋ฐฐ์ด์ ํฉ ๊ตฌํ ๋ ๋ง์ด ์ฌ์ฉ
let numbers = [1, 2, 3, 4, 5, 6];
let reduce = numbers.reduce(function (sum, item, index, arr) {
console.log(sum, item, index, arr);
return sum + item;
});
โ [].filter((๋งค๊ฐ๋ณ์) => ์กฐ๊ฑด);
const words = [
"spray",
"limit",
"elite",
"exuberant",
"destruction",
"present",
];
let arr = words.filter((item) => item.length >= 6);
console.log(arr);
โ [].includes(์์);
const words = [
"spray",
"limit",
"elite",
"exuberant",
"destruction",
"present",
];
console.log(words.includes("spray"));
console.log(words.includes("hi"));
์์ )
๋ ๋ฐฐ์ด์์ ๋์ผํ ์์๋ง ๊ฐ์ง๋ ๋ฐฐ์ด same ์์ฑ
๋ ๋ฐฐ์ด์์ ์๋ก ๋ค๋ฅธ ์์๋ง์ ๊ฐ์ง๋ ๋ฐฐ์ด diff ์์ฑ
let fruits1 = ["์ฌ๊ณผ", "๋ธ๊ธฐ", "ํ์ธ์ ํ", "์๋ฐ", "์ฐธ์ธ", "์ค๋ ์ง", "์๋", "๋ง๊ณ "];
let fruits2 = ["์๋ฐ", "์ฌ๊ณผ", "์ฐธ์ธ", "์ค๋ ์ง", "ํ์ธ์ ํ", "๋ง๊ณ "];
let same = fruits1.filter((item) => fruits2.includes(item));
console.log(same);
//forEach ์ฌ์ฉ
fruits1.forEach(function (item) {
if (fruits2.includes(item)) {
same.push(item);
}
});
let diff = [];
diff = fruits1.filter((item) => !fruits2.includes(item));
//forEach ์ฌ์ฉ
fruits1.forEach(function (item) {
if (!fruits2.includes(item)) {
diff.push(item);
}
});
console.log(diff);
โ find/ findIndex
find: ๋ฐฐ์ด์ ํน์ ๊ฐ์ด ์๋์ง ์ฐพ๊ณ ๋ฐํ
findIndex: ๋ฐฐ์ด์ ํน์ ๊ฐ์ด ์๋์ง ์ฐพ๊ณ index ๋ฐํ
๐ Object ๊ฐ์ฒด
โ object.assign()
๊ฐ์ฒด์ ๋ณํฉ์ ์ฌ์ฉ๋๋ ๋ฉ์๋
์ ์ ๋ฉ์๋ -> ๊ธฐ์กด์ ์ ์ธ๋ ๊ฐ์ฒด์ ์ฌ์ฉํ์ง ์๊ณ ๊ฐ์์Object ๋ผ๋ ๊ฒ์ ๋ถ๋ฌ์์ ์ฌ์ฉ
let obj1 = { a: 1, b: 2 };
let obj2 = { b: 3, c: 4 };
let assignedObj = Object.assign(obj1, obj2);
console.log(assignedObj); // { a: 1, b: 3, c: 4 };
console.log(obj1); // { a: 1, b: 3, c: 4 };
console.log(obj2); // { b: 3, c: 4 }
โ๏ธ ๊ฐ์ฒด์ ๋ถ๋ณ์ฑ
const userName = {
id: 1,
name: "haru",
};
const userEmail = {
id: 1,
email: "***@naver.com",
};
const assignedObj = Object.assign(userName, userEmail);
console.log(assignedObj);
console.log(userName);
console.log(assignedObj === userName); // true
const a = { x: "y" };
const b = { x: "y" };
console.log(a === b); // false
๋ฐ์ดํฐ๊ฐ ๊ฐ์๊ฐ? X
๋ฉ๋ชจ๋ฆฌ ์ฃผ์๊ฐ ๊ฐ์๊ฐ? O
// object์ ๋ฃ์ด์ฃผ๋ฉด ๋ฉ๋ชจ๋ฆฌ๊ฐ ๋ค๋ฅด๊ธฐ ๋๋ฌธ์ false ๋ฐํ
const assignedObj = Object.assign({}, userName, userEmail);
Object.assign(userName, userEmail);
console.log(assignedObj);
console.log(userName);
console.log(assignedObj === userName); // false
โ object.key()
๊ฐ์ฒด์ key ๊ฐ์ ๋ฐฐ์ด๋ก ๋ฐํํด ์ฃผ๋ ๋ฉ์๋
const user = {
id: 1,
name: "Haru",
email: "***@naver.com",
};
const keys = Object.keys(user);
console.log(keys); // ["id", "name", "email"]
const values = keys.map((key) => user[key]);
console.log(values); // ["1", "Haru", "***@naver.com"]
'Client > Javascript' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[JavaScript] JS์์ Module ์ฌ์ฉํ๊ธฐ (0) | 2022.08.29 |
---|---|
JSDoc ๋? (0) | 2022.08.29 |
[JavaScript] DOM๊ณผ DOM API (0) | 2022.07.15 |
[JavaScript] JS ๊ธฐ์ด ๋ง๋ฌด๋ฆฌ (0) | 2022.07.15 |
[JavaScript] JS ๊ธฐ์ด (0) | 2022.07.13 |