๐Ÿฆœ Computer Language/JavaScript

[JavaScript] ๋ฐฐ์—ด ๋‚ด์žฅ ํ•จ์ˆ˜

forEach

๋ฐฐ์—ด์˜ ์š”์†Œ๋“ค์„ ๋ฐ˜๋ณตํ•˜์—ฌ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ๋‹ค. 

forEach ๊ตฌ๋ฌธ์˜ ์ธ์ž๋กœ ์ฝœ๋ฐฑ ํ•จ์ˆ˜๋ฅผ ๋“ฑ๋กํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ, ๋ฐฐ์—ด์˜ ์š”์†Œ๋ฅผ ์ˆœํšŒํ•  ๋•Œ ํ•ด๋‹น ์ฝœ๋ฐฑ ํ•จ์ˆ˜๊ฐ€ ํ˜ธ์ถœ๋œ๋‹ค. ์ด๋•Œ, ๋ฐฐ์—ด ์š”์†Œ์˜ ์ธ๋ฑ์Šค์™€ ๊ฐ’์— ์ ‘๊ทผ ๊ฐ€๋Šฅํ•˜๋‹ค.

const arr = [1, 2, 3, 4, 5];
arr.forEach((elm) => console.log(elm * 2));

arr.forEach(function(item, index){
	console.log(item, index);
}); // item: ๋ฐฐ์—ด ๊ฐ๊ฐ์˜ ์•„์ดํ…œ & index: ๋ฐฐ์—ด ์•„์ดํ…œ์˜ index

 

map

์›๋ณธ ๋ฐฐ์—ด์˜ ๋ชจ๋“  ์š”์†Œ๋ฅผ ์ˆœํšŒํ•˜๋ฉฐ ์—ฐ์‚ฐ์„ ํ•œ ํ›„, return ๊ฐ’์œผ๋กœ ํ•ด๋‹น ์—ฐ์‚ฐ์˜ ๊ฒฐ๊ณผ๋ฅผ ๋ฐ›์•„์˜ฌ ์ˆ˜ ์žˆ๋‹ค.

const arr = [1, 2, 3, 4, 5];
const newArr = arr.map((elm) => {
	return elm * 2;
});

console.log(newArr); // [2, 4, 6, 8, 10]

 

includes

ํŠน์ •ํ•œ ๊ฐ’์ด ๋ฐฐ์—ด ์•ˆ์— ์กด์žฌํ•˜๋Š” ์ง€ ํ™•์ธํ•˜๊ณ  ์‹ถ์„ ๋•Œ ์‚ฌ์šฉ ๊ฐ€๋Šฅ

#2 Case๋ฅผ ๋ณด๋ฉด, includes๋Š” === ์—ฐ์‚ฐ์ฒ˜๋Ÿผ type์ด ๊ฐ™์€์ง€ ์—ญ์‹œ ํ™•์ธํ•˜๋ฏ€๋กœ, 1์ด ์•„๋‹Œ ๋ฌธ์ž์—ด "1"์ด ์ „๋‹ฌ๋˜๋ฉด false๋ฅผ return ํ•œ๋‹ค.

const arr = [1, 2, 3, 4, 5];

// #1 Case
let num = 1;
console.log(arr.includes(num)); // true

// #2 Case
let str = "1";
console.log(arr.includes(str)); // false

 

indexOf

includes์™€ ๋™์ผํ•œ ์—ญํ•  + ๋ช‡ ๋ฒˆ์งธ index์— ์œ„์น˜ํ•˜๋Š” ์ง€

์กด์žฌํ•˜์ง€ ์•Š๋Š”๋‹ค๋ฉด? → return -1

 

find

๋ฐฐ์—ด์—์„œ ํ•ด๋‹น ํŒ๋ณ„ ํ•จ์ˆ˜๋ฅผ ๋งŒ์กฑํ•˜๋Š” ์ฒซ ์š”์†Œ๋ฅผ return

์›ํ•˜๋Š” ์š”์†Œ ์ฐพ์œผ๋ฉด? → Method ์ข…๋ฃŒ

์กด์žฌํ•˜์ง€ ์•Š๋Š”๋‹ค๋ฉด? → return undefinded

 

findIndex

๋ฐฐ์—ด์—์„œ ํ•ด๋‹น ํŒ๋ณ„ ํ•จ์ˆ˜๋ฅผ ๋งŒ์กฑํ•˜๋Š” ์ฒซ ์š”์†Œ์— ๋Œ€ํ•œ index return

์กด์žฌํ•˜์ง€ ์•Š๋Š”๋‹ค๋ฉด? → return -1

 

join

๋ฐฐ์—ด์˜ ๋ชจ๋“  ๋ฌธ์ž์—ด์„ ํ•ฉ์ณ์ฃผ๋Š” ๋ฉ”์†Œ๋“œ

๊ตฌ๋ถ„์ž: join()์˜ ์ธ์ž๋กœ ๋“ค์–ด๊ฐ€๋Š” ๊ฐ’ 

const arr = ["๊ณ ์–‘์ด", "๊ฐ•์•„์ง€", "๋ฉ๋ฉ์ด", "์™ˆ์™ˆ์ด"];
console.log(arr.join(' ')); // ๊ณ ์–‘์ด ๊ฐ•์•„์ง€ ๋ฉ๋ฉ์ด ์™ˆ์™ˆ์ด

 

filter

ํŠน์ • ์กฐ๊ฑด์„ ๋งŒ์กฑํ•˜๋Š” ์ƒˆ๋กœ์šด ๋ฐฐ์—ด์ด ํ•„์š”ํ•  ๋•Œ ์ฃผ๋กœ ์‚ฌ์šฉ, ๋ง๊ทธ๋Œ€๋กœ ํ•„ํ„ฐ๋ง ์—ญํ• 

์ˆœ์ฐจ์ ์œผ๋กœ ๊ฐ’์„ ์ ‘๊ทผํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์ˆœํšŒ๋ฅผ ์œ„ํ•œ for ๋ฌธ ๊ฐ™์€ ์ฝ”๋“œ ์ž‘์„ฑ์ด ๋ถˆํ•„์š”ํ•˜๋‹ค.

const arr = [1, 2, 3, 4, 5]; 
const result = arr.filter(num => num > 3); 

console.log(result); // [4, 5]

json๊ณผ ๊ฐ™์€ ๊ฐ์ฒด์—๋„ ํšจ๊ณผ์ ์œผ๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. 

const person = [
	{name: "a", money: 900000},
    {name: "b", money: 300000},
    {name: "c", money: 700000},
    {name: "d", money: 400000}
]

const rich = person.filter(p => p.money > 500000);

console.log(rich); // [{name: "a", money: 900000}, {name: "c", money: 700000}]

const richName = rich.map(p => p.name);

console.log(richName); // ["a", "c"]

 

slice

๋ฐฐ์—ด์„ ์ž๋ฅด๊ณ  ์‹ถ์„ ๋•Œ ์‚ฌ์šฉํ•˜๋ฉฐ, ์ž๋ฅธ ๋ฐฐ์—ด์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

const person = [
	{name: "a", money: 900000},
    {name: "b", money: 300000},
    {name: "c", money: 700000},
    {name: "d", money: 400000}
]

console.log(person.slice());
console.log(person.slice(0,2));

/* ์ถœ๋ ฅ:
[ { name: 'a', money: 900000 },
  { name: 'b', money: 300000 },
  { name: 'c', money: 700000 },
  { name: 'd', money: 400000 } ]
  
[ { name: 'a', money: 900000 }, { name: 'b', money: 300000 } ] */

 

concat

๋ฐฐ์—ด์„ ๋ถ™์ด๊ณ  ์‹ถ์„ ๋•Œ ์‚ฌ์šฉํ•˜๋ฉฐ, ๊ฒฐ๊ณผ ๋ฐฐ์—ด์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

const arr1 = [
  { num: 1, color: "red" },
  { num: 2, color: "green" },
  { num: 3, color: "blue" }
];

const arr2 = [
  { num: 4, color: "red" },
  { num: 5, color: "white" }
];

console.log(arr1.concat(arr2));

/*
์ถœ๋ ฅ: 
[ { num: 1, color: 'red' },
  { num: 2, color: 'green' },
  { num: 3, color: 'blue' },
  { num: 4, color: 'red' },
  { num: 5, color: 'white' } ] */