๐ŸŒฑ → ๐ŸŒณ

[JavaScript] JavaScript ๊ธฐ์ดˆ ๋ณธ๋ฌธ

Client/Javascript

[JavaScript] JavaScript ๊ธฐ์ดˆ

BAY 2022. 7. 22. 18:45
728x90

๐Ÿ“Œ ๋ฌธ์ž์—ด ๊ด€๋ จ 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"]

 

 

728x90

'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