๐ด Array.of
โช ๋ฐฐ์ด ๋ง๋ค๊ธฐ

const friends = Array.of("a", "b", "c", "d");
console.log(friends); // ["a", "b", "c", "d"]
๐ Array.from
โช Array-like object๋ฅผ Array๋ก ๋ง๋ค๊ธฐ

// ์ฌ์ ์ HTML์ btn์ด๋ผ๋ class๋ฅผ ๊ฐ๋ button 10๊ฐ๋ฅผ ์์ฑํ๋ค.
const buttons = document.getElementByClass("btn");
Array.from(buttons).forEach(button => {
button.addEventListener("click", () => console.log("I ve been clicked"));
};
๐ก Array.find && Array.findIndex
โช Array.find ๋ฐฐ์ด์ ๊ฐ ์์๋ฅผ ํ์ธํ๋ฉด์ ์กฐ๊ฑด์ ๋ง๋ ์ฒซ ๋ฒ์งธ ์์๋ฅผ ๋ฆฌํดํ๋ค.
โช Array.findIndex ๋ฐฐ์ด์ ๊ฐ ์์๋ฅผ ํ์ธํ๋ฉด์ ์กฐ๊ฑด์ ๋ง๋ ์ฒซ ๋ฒ์งธ ์์์ ์ธ๋ฑ์ค๋ฅผ ๋ฆฌํดํ๋ค.

const friends = [
"jeong@gmail.com",
"seung@naver.com",
"ryong@kakao.com",
"lin@korea.com",
"hyun@hotmail.com"
];
const target = friends.find(friend => friend.includes("@korea.com"));
console.log(target); // lin@korea.com
const index = friends.findIndex(friend => friend.includes("@korea.com"));
console.log(index); // 3
๐ข Array.fill
โช ๋ฐฐ์ด์ ์์๋ฅผ ํ๋ผ๋ฏธํฐ์ ํด๋นํ๋ ์์๋ก ๋ณ๊ฒฝํ๋ค.

const array1 = [1, 2, 3, 4];
// index 2๋ถํฐ index 4 ์ด์ ๊น์ง 0์ผ๋ก ์ฑ์ฐ๊ธฐ
console.log(array1.fill(0, 2, 4));
// output: [1, 2, 0, 0]
// index 1๋ถํฐ ๋๊น์ง 5๋ก ์ฑ์ฐ๊ธฐ
console.log(array1.fill(5, 1));
// output: [1, 5, 5, 5]
console.log(array1.fill(6));
// output: [6, 6, 6, 6]
๐ต Array.flat

const complexArray = [1, [2], [ [8], [8], [ [ [8], [8], [ [6], [5], [3] ] ] ] ] ];
/*
[
1,
[2],
[ [8],
[8],
[ [8],
[8],
[ [6], [5], [3] ]
]
]
]
*/
> [1, [2], [ [8], [8], [ [ [8], [8], [ [6], [5], [3] ] ] ] ] ].flat()
// (5) [1, 2, Array(1), Array(1), Array(1)]
> [1, [2], [ [8], [8], [ [ [8], [8], [ [6], [5], [3] ] ] ] ] ].flat(2)
// (5) [1, 2, 8, 8, Array(3)]
> [1, [2], [ [8], [8], [ [ [8], [8], [ [6], [5], [3] ] ] ] ] ].flat(3)
// (7) [1, 2, 8, 8, Array(1), Array(1), Array(3)]
> [1, [2], [ [8], [8], [ [ [8], [8], [ [6], [5], [3] ] ] ] ] ].flat(4)
// (9) [1, 2, 8, 8, 8, 8, Array(1), Array(1), Array(1)]
> [1, [2], [ [8], [8], [ [ [8], [8], [ [6], [5], [3] ] ] ] ] ].flat(5)
// (9) [1, 2, 8, 8, 8, 8, 6, 5, 3]
๐ฃ Array.sort
โช ์ํ๋ ์กฐ๊ฑด์ ๋ฐ๋ผ sort๋ฅผ ์งํํ๋ฉด ๋ฐฐ์ด๊ฐ ์์ฒด๊ฐ ๋ณ๊ฒฝ๋๋ค.
โช ์กฐ๊ฑด์ ๊ฑธ์ง ์์ ๊ฒฝ์ฐ, ์ซ์ ์ ๋ ฌ์์๋ 9๊ฐ 80๋ณด๋ค ์์ ์ค์ง๋ง ์ซ์๋ ๋ฌธ์์ด๋ก ๋ณํ๋๊ธฐ ๋๋ฌธ์ "80"์ ์ ๋ ์ฝ๋ ์์์์ "9" ์์ ์ค๋ ์ํฉ์ด ๋ฐ์ํ๋ค.

const fruits = ["apple", "strawberry", "avocado"];
// ๋ฐฐ์ด ์์ ๊ธธ์ด ์ค๋ฆ์ฐจ์ ์ ๋ ฌ
const sortFruitByLength = (fruitA, fruitB) => {
return fruitA.length - fruitB.length;
}
console.log(fruits.sort(sortFruitByLength));
// [ 'apple', 'avocado', 'strawberry' ]
// ๋ฐฐ์ด ์์ ๊ธธ์ด ๋ด๋ฆผ์ฐจ์ ์ ๋ ฌ
const sortFruitByLength2 = (fruitA, fruitB) => {
return fruitB.length - fruitA.length;
}
console.log(fruit.sort(sortFruitByLength2));
// [ 'strawberry', 'avocado', 'apple' ]

// ๋ฐฐ์ด ์์ฑ ์ค ํ๋์ ๊ฐ์ ๊ธฐ์ค์ผ๋ก ์ ๋ ฌ
const people = [
{
name: "ryong",
age: 28
},
{
name: "dragon",
age: 32
},
];
// ์ค๋ฆ์ฐจ์ ์ ๋ ฌ
const orderPeopleByAge = (personA, personB) => {
return personA.age - personB.age;
}
console.log(people.sort(orderPeopleByAge));
// [ { name: "ryong", age: 28 }, { name: "dragon", age: 32 } ]
// ๋ด๋ฆผ์ฐจ์ ์ ๋ ฌ
const orderPeopleByAge2 = (personA, personB) => {
return personB.age - personA.age;
}
console.log(people.sort(orderPeopleByAge));
// [ { name: "dragon", age: 32 }, { name: "ryong", age: 28 } ]
๐ค Array.filter
โช ์ฃผ์ด์ง ํจ์์ ํ ์คํธ๋ฅผ ํต๊ณผํ๋ ๋ชจ๋ ์์๋ฅผ ๋ชจ์ ์๋ก์ด ๋ฐฐ์ด๋ก ๋ฐํํ๋ค.

const words = ['spray', 'limit', 'elite', 'exuberant', 'destruction', 'present'];
const result = words.filter(word => word.length > 6);
console.log(result);
// expected output: Array ["exuberant", "destruction", "present"]

const fruits = ['apple', 'banana', 'grapes', 'mango', 'orange'];
/**
* ๊ฒ์ ์กฐ๊ฑด์ ๋ฐ๋ฅธ ๋ฐฐ์ด ํํฐ๋ง(์ฟผ๋ฆฌ)
*/
const filterItems = (query) => {
return fruits.filter((el) =>
el.toLowerCase().indexOf(query.toLowerCase()) > -1
);
}
console.log(filterItems('ap')); // ['apple', 'grapes']
console.log(filterItems('an')); // ['banana', 'mango', 'orange']
๐ด Array.reduce
โช ๋ฐฐ์ด์ ๊ฐ ์์์ ๋ํด ์ฃผ์ด์ง ๋ฆฌ๋์(reducer) ํจ์๋ฅผ ์คํํ๊ณ , ํ๋์ ๊ฒฐ๊ณผ๊ฐ์ ๋ฐํํ๋ค.

const array1 = [1, 2, 3, 4];
// 0 + 1 + 2 + 3 + 4
const initialValue = 0;
const sumWithInitial = array1.reduce(
(previousValue, currentValue) => previousValue + currentValue,
initialValue
);
console.log(sumWithInitial);
// expected output: 10
Array.prototype.reduce() - JavaScript | MDN
reduce() ๋ฉ์๋๋ ๋ฐฐ์ด์ ๊ฐ ์์์ ๋ํด ์ฃผ์ด์ง ๋ฆฌ๋์(reducer) ํจ์๋ฅผ ์คํํ๊ณ , ํ๋์ ๊ฒฐ๊ณผ๊ฐ์ ๋ฐํํฉ๋๋ค.
developer.mozilla.org
๐ Array.concat
โช ์ธ์๋ก ์ฃผ์ด์ง ๋ฐฐ์ด์ด๋ ๊ฐ๋ค์ ๊ธฐ์กด ๋ฐฐ์ด์ ํฉ์ณ์ ์ ๋ฐฐ์ด์ ๋ฐํํ๋ค.

const array1 = ['a', 'b', 'c'];
const array2 = ['d', 'e', 'f'];
const array3 = array1.concat(array2);
console.log(array3);
// expected output: Array ["a", "b", "c", "d", "e", "f"]
๐ก Array.slice
โช ์ด๋ค ๋ฐฐ์ด์ begin๋ถํฐ end๊น์ง(end ๋ฏธํฌํจ)์ ๋ํ ์์ ๋ณต์ฌ๋ณธ์ ์๋ก์ด ๋ฐฐ์ด ๊ฐ์ฒด๋ก ๋ฐํํฉ๋๋ค. ์๋ณธ ๋ฐฐ์ด์ ๋ฐ๋์ง ์์ต๋๋ค.

const animals = ['ant', 'bison', 'camel', 'duck', 'elephant'];
console.log(animals.slice(2));
// expected output: Array ["camel", "duck", "elephant"]
console.log(animals.slice(2, 4));
// expected output: Array ["camel", "duck"]
console.log(animals.slice(1, 5));
// expected output: Array ["bison", "camel", "duck", "elephant"]
console.log(animals.slice(-2));
// expected output: Array ["duck", "elephant"]
console.log(animals.slice(2, -1));
// expected output: Array ["camel", "duck"]
console.log(animals.slice());
// expected output: Array ["ant", "bison", "camel", "duck", "elephant"]
'JavaScript' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[ JS ] Rest And Spread (0) | 2022.07.05 |
---|---|
[ JS ] Destructuring (0) | 2022.07.05 |
[ JS ] Strings (0) | 2022.07.05 |
[ JS ] Functions (0) | 2022.07.05 |
[ JS ] Variables and Operator (0) | 2022.07.05 |