[1]. ๐Ÿ”Ž์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ๋ฐฐ์—ด๋‚ด์žฅํ•จ์ˆ˜ ์ •๋ฆฌ (2)

JunTechยท2020๋…„ 8์›” 17์ผ
2

๐Ÿ”Š ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์˜ ๋ฐฐ์—ด๋‚ด์žฅํ•จ์ˆ˜๋ฅผ ๋ณต์Šตํ•˜๊ณ  ์ •๋ฆฌํ•˜๊ธฐ ์œ„ํ•œ ๊ธ€์ž…๋‹ˆ๋‹ค.

6.๐Ÿ’ก find

find() ๋ฉ”์„œ๋“œ๋Š” ์ฃผ์–ด์ง„ ํŒ๋ณ„ ํ•จ์ˆ˜๋ฅผ ๋งŒ์กฑํ•˜๋Š” ์ฒซ ๋ฒˆ์งธ ์š”์†Œ์˜ ๊ฐ’์„ ๋ฆฌํ„ดํ•ฉ๋‹ˆ๋‹ค. ๋งŒ์•ฝ ์—†์œผ๋ฉด undefined๋ฅผ ๋ฆฌํ„ดํ•ฉ๋‹ˆ๋‹ค.

๊ฐ„๋‹จํ•˜๊ฒŒ ์ฝ”๋“œ๋กœ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค.

const array1 = [5, 12, 8, 130, 44];

const found = array1.find(element => element > 10);

console.log(found);

๊ฒฐ๊ณผ ๊ฐ’: 12

ํŒ๋ณ„ํ•จ์ˆ˜๊ฐ€ ์š”์†Œ๊ฐ€ 10๋ณด๋‹ค ํฐ ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•˜๊ณ  ์žˆ์ง€๋งŒ find ๋ฉ”์„œ๋“œ๋Š” ์ฒซ ๋ฒˆ์งธ ์š”์†Œ๋งŒ์„ ๋ฐ˜ํ™˜ํ•˜๊ธฐ ๋•Œ๋ฌธ์— 12๋งŒ ๋ฐ˜ํ™˜๋œ ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

7.๐Ÿ’ก findIndex

findIndex() ๋ฉ”์„œ๋“œ๋Š” ์ฃผ์–ด์ง„ ํŒ๋ณ„ ํ•จ์ˆ˜๋ฅผ ๋งŒ์กฑํ•˜๋Š” ๋ฐฐ์—ด์˜ ์ฒซ ๋ฒˆ์งธ ์š”์†Œ์— ๋Œ€ํ•œ ์ธ๋ฑ์Šค๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ ‡์ง€ ์•Š๋‹ค๋ฉด -1๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
์ธ๋ฑ์Šค๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค๋Š” ๊ฒƒ์€ ํŒ๋ณ„ ํ•จ์ˆ˜์— ๋งŒ์กฑํ•˜๋Š” ์ฒซ ๋ฒˆ์งธ ๊ฐ’์˜ '์ˆœ์„œ'๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค๊ณ  ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

const array1 = [5, 12, 8, 130, 44];

const isLargeNumber = (element) => element > 13;

console.log(array1.findIndex(isLargeNumber));

๋‹ค์Œ๊ณผ ๊ฐ™์€ ์ฝ”๋“œ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ฐฐ์—ด์—์„œ ํŒ๋ณ„ ํ•จ์ˆ˜ isLargeNumber์— ํ•ด๋‹นํ•˜๋Š” ์ฒซ ๋ฒˆ์งธ ์š”์†Œ๊ฐ€ 130์ž…๋‹ˆ๋‹ค. 130์˜ index๋Š” 3์ด๊ธฐ ๋•Œ๋ฌธ์— ์ด ์ฝ”๋“œ์˜ ์ฝ˜์†”๋กœ๊ทธ์—๋Š” '3'์ด ์ถœ๋ ฅ๋  ๊ฒƒ ์ž…๋‹ˆ๋‹ค.

8.๐Ÿ’ก flat

flat() ๋ฉ”์„œ๋“œ๋Š” ๋ชจ๋“  ํ•˜์œ„ ๋ฐฐ์—ด ์š”์†Œ๋ฅผ ์ง€์ •ํ•œ ๊นŠ์ด๊นŒ์ง€ ์žฌ๊ท€์ ์œผ๋กœ ์ด์–ด๋ถ™์ธ ์ƒˆ๋กœ์šด ๋ฐฐ์—ด์„ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค. ๊นŠ์ด์˜ default๋Š” 1์ด๋ฉฐ ๋”ฐ๋กœ ์ง€์ •์ด ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.

์ค‘์ฒฉ ๋ฐฐ์—ด ํ‰ํƒ„ํ™”

const arr1 = [1, 2, [3, 4]];
arr1.flat(); 
// [1, 2, 3, 4]

const arr2 = [1, 2, [3, 4, [5, 6]]];
arr2.flat();
// [1, 2, 3, 4, [5, 6]]

const arr3 = [1, 2, [3, 4, [5, 6]]];
arr3.flat(2);
// [1, 2, 3, 4, 5, 6]

const arr4 = [1, 2, [3, 4, [5, 6, [7, 8, [9, 10]]]]];
arr4.flat(Infinity);
// [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

๋ฐฐ์—ด.flat(๊นŠ์ด ์ง€์ •)์˜ ํ˜•ํƒœ์ด๋ฉฐ ์ง€์ •ํ•œ ๊นŠ์ด์— ๋”ฐ๋ผ ํ•˜์œ„ ๋ฐฐ์—ด๋ถ€ํ„ฐ ์ฐจ๋ก€๋กœ ์ด์–ด๋ถ™ํžˆ๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ๋˜ํ•œ flat์€ ๋น„์–ด์žˆ๋Š” ๋ฐฐ์—ด์˜ ๊ตฌ๋ฉ๋„ ์ œ๊ฑฐํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.

9.๐Ÿ’ก flatMap

flatMap()์€ ๊ฐ ์š”์†Œ์— ๋Œ€ํ•ด map ์ˆ˜ํ–‰ ํ›„, ๊ฒฐ๊ณผ๋ฅผ ์ƒˆ๋กœ์šด ๋ฐฐ์—ด๋กœ ํ‰ํƒ„ํ™”ํ•ฉ๋‹ˆ๋‹ค. ์„ค๋ช…์ด ์–ด๋ ค์›Œ ์˜ˆ์ œ๋ฅผ ๋ณด๋ฉฐ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค.

Map๊ณผ flatMap์˜ ์ฐจ์ด

let arr1 = [1, 2, 3, 4];

arr1.map(x => [x * 2]); 
// [[2], [4], [6], [8]]

arr1.flatMap(x => [x * 2]);
// [2, 4, 6, 8]

// ํ•œ ๋ ˆ๋ฒจ๋งŒ ํ‰ํƒ„ํ™”๋จ
arr1.flatMap(x => [[x * 2]]);
// [[2], [4], [6], [8]]

flatMap์ด ๋ช‡ ๋ ˆ๋ฒจ๋กœ ํ‰ํƒ„ํ™”๋ฅผ ํ•  ์ง€์— ๋”ฐ๋ผ ๋ฐฐ์—ด์˜ ๋ถ„๋ฆฌ๊ฐ€ ๋‹ฌ๋ผ์ง„ ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

let arr1 = ["it's Sunny in", "", "California"];

arr1.map(x=>x.split(" "));
// [["it's","Sunny","in"],[""],["California"]]

arr1.flatMap(x => x.split(" "));
// ["it's","Sunny","in","California"]

map() ๋ฉ”์„œ๋“œ์™€ flatMap()๋ฉ”์„œ๋“œ์˜ ์ฐจ์ด๋ฅผ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. map์€ ํ‰ํƒ„ํ™”๊ฐ€ ์ด๋ฃจ์–ด ์ง€์ง€ ์•Š๊ณ  ๊ณต๋ฐฑ๋„ ๋ฐฐ์—ด ์š”์†Œ์ค‘ ํ•˜๋‚˜๋กœ ๊ปด ์žˆ์ง€๋งŒ flatMap()์€ ํ•œ ๋ฐฐ์—ด๋กœ ํ‰ํƒ„ํ™”๊ฐ€ ์ด๋ฃจ์–ด์ ธ ์žˆ๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

10.๐Ÿ’ก forEach

forEach()๋ฉ”์„œ๋“œ๋Š” ์ฃผ์–ด์ง„ ํ•จ์ˆ˜๋ฅผ ๋ฐฐ์—ด ์š”์†Œ ๊ฐ๊ฐ์— ๋Œ€ํ•ด ์‹คํ–‰ํ•ฉ๋‹ˆ๋‹ค. ๋ฐฐ์—ด ์š”์†Œ ๊ฐ ๊ฐœ๊ฐœ์˜ ๋ชจ๋“  ๊ฐ’์„ ๋”ฐ๋กœ ๋”ฐ๋กœ ์‹คํ–‰ํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.

const array1 = ['b', 'y', 'e'];

array1.forEach(element => console.log(element));

// expected output: "b"
// expected output: "y"
// expected output: "e"

array1์˜ ์š”์†Œ๋“ค์ด ๊ฐ๊ฐ ๋”ฐ๋กœ ์ถœ๋ ฅ์ด ๋œ ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
forEach๋Š” ๋ฐฐ์—ด์„ ๋ณ€ํ˜•ํ•˜์ง€ ์•Š์ง€๋งŒ ์ฝœ๋ฐฑํ•จ์ˆ˜๊ฐ€ ๋ณ€ํ˜•ํ•  ์ˆ˜๋Š” ์žˆ์Šต๋‹ˆ๋‹ค. forEach๋Š” ์ฝœ๋ฐฑํ•จ์ˆ˜๋ฅผ ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ํ•œ ๋ฒˆ์”ฉ ์‹คํ–‰ํ•˜๋ฉฐ ์‚ญ์ œํ–ˆ๊ฑฐ๋‚˜ ์ดˆ๊ธฐํ™”ํ•˜์ง€ ์•Š์€ ์ธ๋ฑ์Šค ์†์„ฑ์— ๋Œ€ํ•ด์„œ๋Š” ์‹คํ–‰ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.

const arraySparse = [1,3,,7]
let numCallbackRuns = 0

arraySparse.forEach(function(element){
  console.log(element)
  numCallbackRuns++
})

console.log("numCallbackRuns: ", numCallbackRuns)

// 1
// 3
// 7
// numCallbackRuns: 3
// 3๊ณผ 9 ์‚ฌ์ด์˜ ์—†๋Š” ๋ฐฐ์—ด์€ forEach๊ฐ€ ์ƒ๋žตํ•˜๋Š” ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

ํ•จ์ˆ˜๋ฅผ ์‚ดํŽด๋ณด๋ฉด forEach๊ฐ€ ๋ฐฐ์—ด์„ ์ˆœํšŒํ•˜๋ฉฐ ๊ฐ๊ฐ์˜ ๊ฐ’์„ ์ถœ๋ ฅํ•˜๊ณ  ์ˆœํšŒํ•  ๋•Œ๋งˆ๋‹ค numCallbackRuns๊ฐ€ 1์”ฉ ์ƒ์Šนํ•ฉ๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ๋งˆ์ง€๋ง‰์— ์ถœ๋ ฅ๋˜๋Š” ๊ฐ’์ด 3์ธ๊ฒƒ์„ ํ™•์ธํ•˜์—ฌ ๋น„์–ด์žˆ๋Š” ๋ฐฐ์—ด์€ forEach๊ฐ€ ์ƒ๋žต์„ ํ•˜๊ณ  ์ˆœํšŒํ•˜๋Š” ๊ฒƒ์„ ์‚ดํŽด๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์šฐ๋ฆฌ๋Š” ์ด forEach๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ฐฐ์—ด์„ ์ˆœํšŒํ•  ๋•Œ for๋ฌธ ๋Œ€์‹  ๊ฐ„ํŽธํ•˜๊ฒŒ forEach๋ฌธ์œผ๋กœ ๋Œ€์‹ ํ•  ์ˆ˜ ์žˆ์„ ๊ฒƒ ์ž…๋‹ˆ๋‹ค.

const items = ['item1', 'item2', 'item3'];
const copy = [];

// ์ด์ „
for (let i=0; i<items.length; i++) {
  copy.push(items[i]);
}

// ์ดํ›„
items.forEach(function(item){
  copy.push(item);
});

๊ฐ„๋‹จํ•˜๊ฒŒ ์ฝœ๋ฐฑํ•จ์ˆ˜๋กœ item๋“ค์„ ๊ฐ๊ฐ copy๋ฐฐ์—ด์— ํ‘ธ์‰ฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

11.๐Ÿ’ก includes

includes() ๋ฉ”์„œ๋“œ๋Š” ๋ฐฐ์—ด์ด ํŠน์ • ์š”์†Œ๋ฅผ ํฌํ•จํ•˜๊ณ  ์žˆ๋Š”์ง€ ํŒ๋ณ„ํ•ฉ๋‹ˆ๋‹ค.

const array1 = [1, 2, 3];

console.log(array1.includes(2));
// expected output: true

const pets = ['cat', 'dog', 'bat'];

console.log(pets.includes('cat'));
// expected output: true

console.log(pets.includes('at'));
// expected output: false

includeํ•จ์ˆ˜์˜ ๋งค๊ฐœ๋ณ€์ˆ˜๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

arr.includes(valueToFind[, fromIndex])

valueToFind๋Š” ํƒ์ƒ‰ํ•  ์š”์†Œ์ด๊ณ  fromIndex๋Š” ์–ด๋””์„œ ๋ถ€ํ„ฐ ๊ฒ€์ƒ‰์„ ์‹œ์ž‘ํ•  ์ง€ ์ •ํ•˜๋Š” ๋งค๊ฐœ๋ณ€์ˆ˜ ์ž…๋‹ˆ๋‹ค. ๋งŒ์•ฝ fromIndex๊ฐ€ ์Œ์ˆ˜์ด๋ฉด ์ „์ฒด ๊ฒ€์ƒ‰์„ ์‹œ์ž‘ํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. fromIndex๊ฐ€ ๋ฐฐ์—ด์˜ ํฌ๊ธฐ๋ณด๋‹ค ํฌ๋ฉด false๋ฅผ ๋ฐ˜ํ™˜ํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ์˜ˆ์ œ๋ฅผ ๋ด…๋‹ˆ๋‹ค.

[1, 2, 3].includes(2);     // true
[1, 2, 3].includes(4);     // false
[1, 2, 3].includes(3, 3);  // false
[1, 2, 3].includes(3, -1); // true
[1, 2, NaN].includes(NaN); // true

3๋ฒˆ๊ณผ 4๋ฒˆ์„ ๋ณด๋ฉด 3๋ฒˆ์€ 3์€ ๋ฐฐ์—ด์•ˆ์— ํฌํ•จ๋˜์–ด ์žˆ์ง€๋งŒ fromIndex๊ฐ€ 3์ด๋ฏ€๋กœ ๋ฐฐ์—ด์˜ ํฌ๊ธฐ๋ณด๋‹ค ํฌ๋ฏ€๋กœ false๊ฐ€ ๋ฆฌํ„ด๋˜๊ณ  4๋ฒˆ์€ fromIndex๊ฐ€ -1์ด๊ธฐ ๋•Œ๋ฌธ์— ์ „์—ญ ๊ฒ€์ƒ‰์„ ํ•˜๊ฒŒ ๋˜์–ด 3์„ ์ฐพ์•„ true๊ฐ€ ๋ฆฌํ„ด๋œ ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

๐Ÿ“ƒ ์ฐธ๊ณ ๋ฌธํ—Œ

Mozilla MDN

profile
์ข‹์€ Front-end ๊ฐœ๋ฐœ์ž๊ฐ€ ๋˜๊ธฐ ์œ„ํ•ด ๋…ธ๋ ฅํ•˜๋Š” ํ•™์ƒ์ž…๋‹ˆ๋‹ค.

0๊ฐœ์˜ ๋Œ“๊ธ€