๐โโ๏ธ ๋ฐฐ์ด(Array)์ด๋,
์ด๋ฆ๊ณผ ์ธ๋ฑ์ค๋ก ์ฐธ์กฐ๋๋ ์ ๋ ฌ๋ ๊ฐ์ ์งํฉ. ๋ฐฐ์ด์ ๊ตฌ์ฑํ๋ ๊ฐ๊ฐ์ ๊ฐ์ ๋ฐฐ์ด ์์(element)๋ผ๊ณ ํ๋ฉฐ, ๋ฐฐ์ด์์์ ์์น๋ฅผ ๊ฐ๋ฆฌํค๋ ์ซ์๋ฅผ ์ธ๋ฑ์ค(index)๋ผ๊ณ ํจ.
์๋ฐ์คํฌ๋ฆฝํธ์ "๋ชจ๋ ๊ฐ"์ ๋ฐฐ์ด์ ์์๊ฐ ๋ ์ ์์
const arr = ['๐', '๐', '๐ฅ'];
console.log(arr);
const arr2 = new Array();
console.log(arr2); // []
const arr3 = new Array(10);
console.log(arr3); // [ <10 empty items> ]
const arr4 = new Array('๐ ', '๐ง', '๐ฅ', '๐');
console.log(arr4);
console.log(Array.of(10)); // [ 10 ]
console<.log(Array.of(1, 2, 3)); // [ 1, 2, 3 ]
console.log(Array.of("Hi", "there!")); // [ 'Hi', 'there!' ]
console.log(arr[0]); // ๐
console.log(arr[1]); // ๐
console.log(arr[2]); // ๐ฅ
console.log(arr.length);
for(let i = 0; i < arr.length; i++) {
console.log(arr[i]);
}
console.log(typeof arr); // object
- ์ผ๋ฐ์ ์ธ ๋ฐฐ์ด : ๊ฐ ์์๊ฐ ๋์ผํ ๋ฐ์ดํฐ ํฌ๊ธฐ๋ฅผ ๊ฐ์ง๋ฉฐ, ๋นํ ์์ด ์ฐ์์ ์ผ๋ก ์ด์ด์ ธ ์์ด ์ธ๋ฑ์ค๋ฅผ ํตํด ์์์ ์์์ ํ ๋ฒ์ ์ ๊ทผํ ์ ์๋ ๊ณ ์ ๋์์ด ์ฅ์
- ์๋ฐ์คํฌ๋ฆฝํธ์ ๋ฐฐ์ด : ์ผ๋ฐ์ ์ธ ๋ฐฐ์ด์ ๋์์ ํ๋ด๋ธ ํน์ํ ๊ฐ์ฒด. ๊ฐ๊ฐ์ ๋ฉ๋ชจ๋ฆฌ ๊ณต๊ฐ์ด ๋์ผํ ํฌ๊ธฐ๋ฅผ ๊ฐ์ง ์์๋ ๋๊ณ ์ฐ์์ ์ผ๋ก ์ด์ด์ ธ ์์ง ์์ ์๋ ์์. ์ธ๋ฑ์ค๋ก ๋ฐฐ์ด ์์์ ์ ๊ทผํ๋ ๊ฒฝ์ฐ ์ผ๋ฐ์ ์ธ ๋ฐฐ์ด๋ณด๋ค ๋๋ฆฌ์ง๋ง ์์์ ์ฝ์ , ์ญ์ ์ ๊ฒฝ์ฐ ๋น ๋ฆ.
// ์๋ฐ์คํฌ๋ฆฝํธ์ ๋ชจ๋ ๊ฐ์ด ๊ฐ์ฒด์ ํ๋กํผํฐ ๊ฐ์ด ๋ ์ ์์ผ๋ฏ๋ก
// ๋ชจ๋ ๊ฐ์ด ๋ฐฐ์ด์ ์์๊ฐ ๋ ์ ์์
const arr = [
'์น์ฆ',
'2',
true,
null,
undefined,
NaN,
Infinity,
[], // ๋ฐฐ์ด
{}, // ๊ฐ์ฒด
function() {} // ํจ์
];
console.log([].length); // 0
const arr = [1, 2, 3, 4, 5];
console.log(arr.length); // 5
arr.push(6);
console.log(arr); // [ 1, 2, 3, 4, 5, 6 ]
console.log(arr.length); // 6
arr.pop();
console.log(arr); // [ 1, 2, 3, 4, 5 ]
console.log(arr.length); // 5
arr.length = 3;
console.log(arr); // [ 1, 2, 3 ]
console.log(arr.length); // 3
arr.length = 10;
console.log(arr); // [ 1, 2, 3, <7 empty items> ]
console.log(arr.length); // 10
console.log(Object.getOwnPropertyDescriptors(arr));
const sparse = [, 2, , 4];
console.log(sparse); // [ <1 empty item>, 2, <1 empty item>, 4 ]
console.log(sparse.length); // 4
console.log(Object.getOwnPropertyDescriptors(sparse));
๐โโ๏ธ Array.prototype์ ๋ฐฐ์ด์ ์ํ ๋นํธ์ธ ๋ฉ์๋๋ฅผ ์ ๊ณต
const arr = [];
// ๋ฐฐ์ด์ ์์ฑ์ ํจ์๋ Array
console.log(arr.constructor === Array); // true
// ๋ฐฐ์ด์ ํ๋กํ ํ์
๊ฐ์ฒด๋ Array.prototype
console.log(Object.getPrototypeOf(arr) === Array.prototype); // true
// Array.prototype์ด ๋ฐฐ์ด์ ์ํ ๋นํธ์ธ ๋ฉ์๋๋ฅผ ์ ๊ณตํ๋ ๊ฒ ํ์ธ
indexOf()
: ๋ฐฐ์ด์์ ์์๊ฐ ์์นํ ์ธ๋ฑ์ค๋ฅผ ๋ฆฌํด
lastIndexOf()
: ๋ฐฐ์ด์ ์์๊ฐ ์์นํ ๋ง์ง๋ง ์ธ๋ฑ์ค๋ฅผ ๋ฆฌํด
includes()
: ๋ฐฐ์ด์ ํด๋น ์์ ํฌํจ ์ฌ๋ถ ๋ฆฌํด
const foodList = ['๋ฌผํ', '์ผ๊ณํ', '๋๋ฉด', '์๋ฐ', '๋ฌผํ'];
console.log(`foodList.indexOf('๋ฌผํ') : ${foodList.indexOf('๋ฌผํ')}`); // 0
console.log(`foodList.indexOf('๋ฌผํ', 1) : ${foodList.indexOf('๋ฌผํ', 1)}`); // 4 // 1 ์ดํ๋ถํฐ ์ฐพ๊ธฐ
console.log(`foodList.indexOf('์ผ๊ฒน์ด') : ${foodList.indexOf('์ผ๊ฒน์ด')}`); // -1 // -1 : ์ธ๋ฑ์ค๊ฐ ์๋ค๋ ์๋ฏธ
console.log(`foodList.lastIndexOf('๋ฌผํ') : ${foodList.lastIndexOf('๋ฌผํ')}`); // 4
console.log(`foodList.lastIndexOf('๋ฌผํ', 1) : ${foodList.lastIndexOf('๋ฌผํ', 1)}`); // 0 // 1 ์ด์ ๋ถํฐ ๊ฑฐ๊พธ๋ก ์ฐพ๊ธฐ
console.log(`foodList.lastIndexOf('์ผ๊ฒน์ด') : ${foodList.lastIndexOf('์ผ๊ฒน์ด')}`); // -1
console.log(`foodList.includes('๋ฌผํ') : ${foodList.includes('๋ฌผํ')}`); // true
console.log(`foodList.includes('์ผ๊ฒน์ด') : ${foodList.includes('์ผ๊ฒน์ด')}`); // false
push()
: ๋ฐฐ์ด์ ๋งจ ๋ค์ ์์ ์ถ๊ฐ
pop()
: ๋ฐฐ์ด์ ๋งจ ๋ค์ ์์ ์ ๊ฑฐ
const chineseFood= ['์ง์ฅ๋ฉด', '์งฌ๋ฝ', '์ฐ๋'];
console.log(`push ์ chineseFood : ${chineseFood}`); // ์ง์ฅ๋ฉด,์งฌ๋ฝ,์ฐ๋
chineseFood.push('ํ์์ก');
chineseFood.push('์์ฅํผ');
// chineseFood.push('ํ์์ก', '์์ฅํผ'); // ์ธ์๋ฅผ ๋์ดํด์ push๋ ๊ฐ๋ฅ
console.log(`push ํ arr : ${chineseFood}`); // ์ง์ฅ๋ฉด,์งฌ๋ฝ,์ฐ๋,ํ์์ก,์์ฅํผ
console.log(`chineseFood.pop() : ${chineseFood.pop()}`); // ์์ฅํผ // ์์๋ฅผ ๋ฐํํ๋ฉด์ ์ ๊ฑฐ
console.log(`chineseFood.pop() : ${chineseFood.pop()}`); // ํ์์ก
console.log(`chineseFood.pop() : ${chineseFood.pop()}`); // ์ฐ๋
console.log(`pop ํ chineseFood : ${chineseFood}`); // ์ง์ฅ๋ฉด,์งฌ๋ฝ
unshift()
: ๋ฐฐ์ด์ ๋งจ ์์ ์์ ์ถ๊ฐ
shift()
: ๋ฐฐ์ด์ ๋งจ ์ ์์ ์ ๊ฑฐ ํ ๋ฐํ
const chickenList = ['์๋
์นํจ', 'ํ๋ผ์ด๋', 'ํ๋ญ'];
console.log(`unshift ์ chickenList : ${chickenList}`); // ์๋
์นํจ,ํ๋ผ์ด๋,ํ๋ญ
// ํ๋ํ๋ ๋ฃ๋ ๊ฒ๊ณผ ํ๋ฒ์ ์ฌ๋ฌ ๊ฐ๋ฅผ ๋ฃ๋ ๊ฒ์ ์์์์ ์ฐจ์ด๊ฐ ์์ (์ฌ๋ฌ ๊ฐ๋ฅผ ๋ฃ์ผ๋ฉด ํ ๋ฉ์ด๋ฆฌ๋ก ์์ ์ฝ์
)
chickenList.unshift('๊ฐ์ฅ์นํจ');
chickenList.unshift('๋ง๋์นํจ');
// chickenList.unshift('๊ฐ์ฅ์นํจ', '๋ง๋์นํจ'); // ์ธ์๋ฅผ ๋์ดํด์ unshift๋ ๊ฐ๋ฅ
console.log(`unshift ํ chickenList : ${chickenList}`); // ๋ง๋์นํจ,๊ฐ์ฅ์นํจ,์๋
์นํจ,ํ๋ผ์ด๋,ํ๋ญ
console.log(`chickenList.shift() : ${chickenList.shift()}`); // ๋ง๋์นํจ
console.log(`chickenList.shift() : ${chickenList.shift()}`); // ๊ฐ์ฅ์นํจ
console.log(`chickenList.shift() : ${chickenList.shift()}`); // ์๋
์นํจ
console.log(`shift ํ chickenList : ${chickenList}`); // ํ๋ผ์ด๋,ํ๋ญ
concat()
: ๋ ๊ฐ ์ด์์ ๋ฐฐ์ด์ ๊ฒฐํฉ
const idol1 = ['์์ด๋ธ', '์ค๋ง์ด๊ฑธ'];
const idol2 = ['ํธ์์ด์ค', '์์คํ'];
const idol3 = ['๋ธ๋ํํฌ', '๋ ๋๋ฒจ๋ฒณ'];
// ๋ฐฐ์ด๋ช
.concat(๋ฐฐ์ด๋ช
1, ๋ฐฐ์ด๋ช
2, ...)
const mix = idol1.concat(idol2);
const mix2 = idol3.concat(idol1, idol2);
console.log(`idol1 ๊ธฐ์ค์ผ๋ก idol2 ๋ฐฐ์ด์ concat : ${mix}`); // ์์ด๋ธ,์ค๋ง์ด๊ฑธ,ํธ์์ด์ค,์์คํ
console.log(`idol3 ๊ธฐ์ค์ผ๋ก idol1, idol2 ๋ฐฐ์ด์ concat : ${mix2}`); // ๋ธ๋ํํฌ,๋ ๋๋ฒจ๋ฒณ,์์ด๋ธ,์ค๋ง์ด๊ฑธ,ํธ์์ด์ค,์์คํ
slice()
: ๋ฐฐ์ด์ ์์ ์ ํ ์๋ผ๋ด๊ธฐ
splice()
: ๋ฐฐ์ด์ index ์์น์ ์์ ์ ๊ฑฐ ๋ฐ ์ถ๊ฐ
const front = ['HTML', 'CSS', 'JavaScript', 'jQuery'];
// slice(์์์ธ๋ฑ์ค, ์ข
๋ฃ์ธ๋ฑ์ค)
console.log(`front.slice(1, 3) : ${front.slice(1, 3)}`); // CSS,JavaScript
console.log(`front : ${front}`); // HTML,CSS,JavaScript,jQuery
// splice(index, ์ ๊ฑฐ์, ์ถ๊ฐ๊ฐ1, ์ถ๊ฐ๊ฐ2, ...)
console.log(`front.splice(3, 1, "React") : ${front.splice(3, 1, "React")}`); // jQuery
console.log(`front : ${front}`); // HTML,CSS,JavaScript,React
join()
: ๋ฐฐ์ด์ ๊ตฌ๋ถ์๋ก ๊ฒฐํฉํ์ฌ ๋ฌธ์์ด๋ก ๋ฐํ
const snackList = ['์ฌํ', '์ด์ฝ๋ ', '๊ป', '๊ณผ์'];
console.log(`snackList.join() : ${snackList.join()}`); // ์ฌํ,์ด์ฝ๋ ,๊ป,๊ณผ์
console.log(`snackList.join('/') : ${snackList.join('/')}`); // ์ฌํ/์ด์ฝ๋ /๊ป/๊ณผ์ // ์ํ๋ ๊ตฌ๋ถ์('/') ์ ๋ฌ
reverse()
: : ๋ฐฐ์ด์ ์์๋ฅผ ๋ค์ง์ (์๋ณธ ๋ณํ)
console.log(`[1, 2, 3, 4, 5].reverse() : ${[1, 2, 3, 4, 5].reverse()}`); // 5,4,3,2,1
๐โโ๏ธ ๊ณ ์ฐจ ํจ์๋,
ํจ์๋ฅผ ์ธ์๋ก ์ ๋ฌ ๋ฐ๊ฑฐ๋ ํจ์๋ฅผ ๋ฐํํ๋ ํจ์
sort()
: ๋ฐฐ์ด์ ์ ๋ ฌ ๊ธฐ์ค์ผ๋ก ์ ๋ ฌ
let numbers = [];
for(let i = 0; i < 10; i++) {
numbers[i] = Math.floor(Math.random() * 100) + 1; // Math.floor : ๋ด๋ฆผ์ฒ๋ฆฌ
}
console.log(`์ ๋ ฌ ์ numbers : ${numbers}`)
// ์ค๋ฆ์ฐจ์ ์ ๋ ฌ์ด ๊ธฐ๋ณธ์ด๋ฉฐ ์ ๋ ฌ ํ ์ ๋ ฌ ์์๋ฅผ ์ ์ง
// ๋ฐฐ์ด์ ๊ธฐ๋ณธ์ ์ผ๋ก ๋ฌธ์์ด ์ ๋ ฌ์ด ๋๋ฏ๋ก ํ ์๋ฆฌ์, ์ธ์๋ฆฌ ์๊ฐ ์ฌ๋ฐ๋ฅด์ง ์๊ฒ ์ ๋ ฌ๋๋ ๋ชจ์ต์ ๋ณด์
// => ๋ค๋ฅธ ์ ๋ ฌ ๊ธฐ์ค์ ์ฌ์ฉํ๋ ค๋ฉด compare ํจ์๋ฅผ ์ธ์๋ก ์ ๋ฌํด์ผํจ
numbers.sort();
console.log(`์ ๋ ฌ ํ numbers : ${numbers}`)
// ์ซ์ ์ค๋ฆ์ฐจ์ ์ ๋ ฌ
function compare(a, b) {
if(a > b) return 1; // 1 : ์, ๋ค ์๋ฆฌ๋ฅผ ๋ฐ๊ฟ์ผํ๋ค๋ ์๋ฏธ
if(a == b) return 0;
if(a < b) return -1;
}
numbers.sort(compare);
console.log('๋งค๊ฐ๋ณ์๋ก compare ํจ์ ์ ๋ฌํ์ฌ ์ซ์ ์ค๋ฆ์ฐจ์ ์ ๋ ฌ');
console.log(numbers);
// ์ซ์ ๋ด๋ฆผ์ฐจ์ ์ ๋ ฌ (ํจ์๋ฅผ ์ธ์๋ก ๋ฐ๋ก ์ ๋ฌ)
numbers.sort(function(a, b) { return b - a});
numbers.sort((a, b) => b - a);
console.log('์ซ์ ๋ด๋ฆผ์ฐจ์ ์ ๋ ฌ');
console.log(numbers);
forEach()
: for๋ฅผ ๋์ฒดํ ์ ์๋ ๊ณ ์ฐจํจ์
๋ฐฐ์ด.forEach(function(item, index, array){ // ๋ฐฐ์ด ์์ ๊ฐ๊ฐ์ ์คํํ ๊ธฐ๋ฅ ์์ฑ });
numbers = [1, 2, 3, 4, 5];
numbers.forEach(function(item, index, array){
console.log(`item : ${item}`)
console.log(`index : ${index}`)
console.log(`array : ${array}`)
});
// ๊ฐ ์์ ๋ณ๋ก * 10ํ ๊ฐ์ ์ฝ์์ ์ถ๋ ฅ
numbers.forEach(item => console.log(item * 10));
map()
: ๋ฐฐ์ด ์์ ์ ์ฒด๋ฅผ ๋์์ผ๋ก ์ฝ๋ฐฑ ํจ์ ํธ์ถ ํ ๋ฐํ ๊ฐ๋ค๋ก ๊ตฌ์ฑ๋ ์๋ก์ด ๋ฐฐ์ด ๋ฐํ
๋ฐฐ์ด.map(function(item, index, array){ // ๋ฐฐ์ด ์์ ๊ฐ๊ฐ์ ๋ฐํํ ์๋ก์ด ๊ฐ })
const types = [true, 1, 'text'].map(item => typeof item); // item๋ค์ ๋ฐ์ดํฐ ํ์
์ ๋ฐฐ์ด๋ก
console.log(types); // [ 'boolean', 'number', 'string' ]
const lengths = ['apple๐', 'banana๐', 'cat๐ฑ', 'dog๐ถ', 'egg๐ณ'].map(item => item.length);
console.log(lengths); // [ 7, 8, 5, 5, 5 ] => ์ด๋ชจ์ง๋ length๊ฐ 2 !
// Array.prototype.filter : ๋ฐฐ์ด ์์ ์ ์ฒด๋ฅผ ๋์์ผ๋ก ์ฝ๋ฐฑ ํจ์ ํธ์ถ ํ ๋ฐํ ๊ฐ์ด true์ธ ์์๋ก๋ง ๊ตฌ์ฑ ๋ ์๋ก์ด ๋ฐฐ์ด ๋ฐํ
const odds = numbers.filter(item => item % 2); // 1์ด๋ผ๋ ๊ฐ๋ง true?
console.log(odds); // [ 1, 3, 5 ] => ํ์๋ก๋ง ๊ตฌ์ฑ๋ ๋ฐฐ์ด ์ถ๋ ฅ
reduce()
: ๋ฐฐ์ด์ ์ํํ๋ฉฐ ๊ฐ ์์์ ๋ํ์ฌ ์ด์ ์ ์ฝ๋ฐฑ ํจ์ ์คํ ๋ฐํ๊ฐ์ ์ ๋ฌํ์ฌ ์ฝ๋ฐฑํจ์๋ฅผ ์์ฑํ๊ณ ๊ทธ ๊ฒฐ๊ณผ๋ฅผ ๋ฐํ
(์ฝ๋ฐฑ ํจ์๊ฐ ๋์ํ ๋ return ํ๋ ๊ฐ์ด ๋ค์ ์ฝ๋ฐฑ ํจ์์ ์ฒซ ๋ฒ์งธ ํ๋ผ๋ฏธํฐ๋ก ์ ๋ฌ)
previouseValue
: ์ด์ ์ฝ๋ฐฑ์ ๋ฐํ๊ฐ
currentValue
: ๋ฐฐ์ด ์์์ ๊ฐ
currentIndex
: ์ธ๋ฑ์ค
array
: ๋ฉ์๋๋ฅผ ํธ์ถํ ๋ฐฐ์ด
numbers.reduce(function(previouseValue, currentValue, currentIndex, array){
console.log(`previouseValue : ${previouseValue}`); // ์ด์ ์ฝ๋ฐฑ ํจ์๊ฐ ๋ฐํํ ๊ฐ
console.log(`currentValue : ${currentValue}`);
console.log(`currentIndex : ${currentIndex}`);
console.log(`array : ${array}`);
});
// ํฉ์ฐ
const sum = [3, 4, 5, 6].reduce(function(previouseValue, currentValue){
return previouseValue + currentValue;
});
console.log(`sum : ${sum}`); // sum : 18
/* ๋ด๊ฐ ์ฐ๋ ํด์ค
1st turn : 0(pv) + 3(cv) = 3(๋ค์ ์ฝ๋ฐฑ์ผ๋ก ๋ฐํ)
2nd turn : 3(pv) + 4(cv) = 7(๋ค์ ์ฝ๋ฐฑ์ผ๋ก ๋ฐํ)
3rd turn : 7(pv) + 5(cv) = 12(๋ค์ ์ฝ๋ฐฑ์ผ๋ก ๋ฐํ)
4th turn : 12(pv) + 6(cv) = 18(๋ค์ ์ฝ๋ฐฑ์ผ๋ก ๋ฐํ)
=> ๋ฐ๋ผ์ ๋ง์ง๋ง ์ฝ๋ฐฑํจ์์ ๋ฆฌํด๊ฐ์ธ 18์ด ์ต์ข
๋ฐํ
*/
// ์ต๋๊ฐ ์ทจ๋
const max = [3, 4, 5, 6].reduce(function(pre, cur){
return pre > cur ? pre : cur;
});
console.log(`max : ${max}`); // max : 6
/* ๋ด๊ฐ ์ฐ๋ ํด์ค
1st turn : 3(pv) > 4(cv) ? 3 : 4; (false์ด๋ฏ๋ก 4๋ฅผ ๋ค์ ์ฝ๋ฐฑ์ผ๋ก ๋ฐํ)
2nd turn : 4(pv) > 5(cv) ? 4 : 5; (false์ด๋ฏ๋ก 5๋ฅผ ๋ค์ ์ฝ๋ฐฑ์ผ๋ก ๋ฐํ)
3rd turn : 5(pv) > 6(cv) ? 5 : 6; (false์ด๋ฏ๋ก 6๋ฅผ ๋ค์ ์ฝ๋ฐฑ์ผ๋ก ๋ฐํ)
=> ๋ฐ๋ผ์ ๋ง์ง๋ง ์ฝ๋ฐฑํจ์์ ๋ฆฌํด๊ฐ์ธ 6์ด ์ต์ข
๋ฐํ
*/
some()
: ๋ฐฐ์ด ๋ด ์ผ๋ถ ์์๊ฐ ์ฝ๋ฐฑ ํจ์์ ํ
์คํธ๋ฅผ ํต๊ณผํ๋์ง ํ์ธํ์ฌ ๊ทธ ๊ฒฐ๊ณผ๋ฅผ boolean์ผ๋ก ๋ฐํ
// ๋ฐฐ์ด ๋ด ์์ ์ค 10๋ณด๋ค ํฐ ๊ฐ์ด 1๊ฐ ์ด์ ์กด์ฌํ๋์ง ํ์ธ
let result = [1, 5, 3, 2, 4].some(item => item > 10);
console.log(`result : ${result}`) // result : false
// ๋ฐฐ์ด ๋ด ์์ ์ค 3๋ณด๋ค ํฐ ๊ฐ์ด 1๊ฐ ์ด์ ์กด์ฌํ๋์ง ํ์ธ
result = [1, 5, 3, 2, 4].some(item => item > 3);
console.log(`result : ${result}`) // result : true (ํ๋๋ผ๋ true๋ฉด ์ ์ฒด์ ์ธ ๊ฒฐ๊ณผ๊ฐ true)
every()
: ๋ฐฐ์ด ๋ด ๋ชจ๋ ์์๊ฐ ์ฝ๋ฐฑ ํจ์์ ํ
์คํธ๋ฅผ ํต๊ณผํ๋์ง ํ์ธํ์ฌ ๊ทธ ๊ฒฐ๊ณผ๋ฅผ boolean์ผ๋ก ๋ฐํ
// ๋ฐฐ์ด ๋ด ์์ ๋ชจ๋ ๊ฐ์ด 3๋ณด๋ค ํฐ์ง ํ์ธ
result = [1, 5, 3, 2, 4].every(item => item > 3);
console.log(`result : ${result}`) // result : false (ํ๋๋ผ๋ false๋ฉด ์ ์ฒด์ ์ธ ๊ฒฐ๊ณผ๊ฐ false)
// ๋ฐฐ์ด ๋ด ์์ ๋ชจ๋ ๊ฐ์ด 0๋ณด๋ค ํฐ์ง ํ์ธ
result = [1, 5, 3, 2, 4].every(item => item > 0);
console.log(`result : ${result}`) // result : true
find()
: ๋ฐฐ์ด์ ์ํํ๋ฉฐ ๊ฐ ์์์ ๋ํ์ฌ ์ธ์๋ก ์ฃผ์ด์ง ์ฝ๋ฐฑ ํจ์๋ฅผ ์คํํ๋ฉฐ ๊ทธ ๊ฒฐ๊ณผ๊ฐ ์ฐธ์ธ ์ฒซ๋ฒ์งธ ์์๋ฅผ ๋ฐํ. ์ฐธ์ธ ์์๊ฐ ์กด์ฌํ์ง ์๋๋ค๋ฉด undefined ๋ฐํ
findIndex()
: ๋ฐฐ์ด์ ์ํํ๋ฉฐ ๊ฐ ์์์ ๋ํ์ฌ ์ธ์๋ก ์ฃผ์ด์ง ์ฝ๋ฐฑ ํจ์๋ฅผ ์คํํ์ฌ ๊ทธ ๊ฒฐ๊ณผ๊ฐ ์ฐธ์ธ ์ฒซ ๋ฒ์ฌ ์์์ ์ธ๋ฑ์ค๋ฅผ ๋ฐํ. ์ฐธ์ธ ์์๊ฐ ์กด์ฌํ์ง ์๋๋ค๋ฉด -1 ๋ฐํ
const students = [
{ name : '์ ์งฑ๊ตฌ', score : 90},
{ name : '์ ์งฑ์', score : 80},
{ name : 'ํฐ๋ฅ์ด', score : 70},
{ name : '์ ์งฑ๊ตฌ', score : 20}
];
result = students.find(item => item.name === '์ ์งฑ๊ตฌ');
console.log(result); // { name: '์ ์งฑ๊ตฌ', score: 90 } (20์ ์งฑ๊ตฌ๊ฐ ์๋ ์ฒซ๋ฒ์งธ ์์์ธ 90์ ์งฑ๊ตฌ๋ฅผ ๋ฐํ)
result = students.findIndex(item => item.name === '์ ์งฑ๊ตฌ');
console.log(result); // 0
// ์กด์ฌํ์ง ์์ ๋,
result = students.find(item => item.name === '์ ํ๋ง');
console.log(result); // undefined
result = students.findIndex(item => item.name === '์ ํ๋ง');
console.log(result); // -1
// find, findIndex๋ ์ผ์นํ๋ ์์๋ฅผ ์ฐพ์ผ๋ฉด ๋ ์ด์ ํ์ํ์ง ์๊ณ ํ๋์ ์์, ์ธ๋ฑ์ค๋ง ๋ฐํ
// ๋ง์ฝ 60์ ์ด์์ธ ํ์๋ค์ ์ฐพ๊ณ ์ถ๋ค๋ฉด?
// filter๋ ์ฝ๋ฐฑํจ์์ ์คํ ๊ฒฐ๊ณผ๊ฐ true์ธ ๋ฐฐ์ด ์์์ ๊ฐ๋ง์ ์ถ์ถํ ์๋ก์ด ๋ฐฐ์ด์ ๋ฐํ
result = students.filter(item => item.score >= 60);
console.log(result); // ๋ชจ๋ 60์ ์ด์์ ํ์๋ค ์ถ๋ ฅ