forEach
: for๋ฌธ ๋์ฒด, ๋ฐฐ์ด ์์ ์๋ ๋ชจ๋ ์์๋ค์ ๋ชจ๋ ์ถ๋ ฅ
const superheroes = ['์์ด์ธ๋งจ', '์บกํด ์๋ฉ๋ฆฌ์นด', 'ํ ๋ฅด', '๋ฅํฐ ์คํธ๋ ์ธ์ง'];
//for๋ฌธ์ผ๋ก ๋ฐฐ์ด ์์ ์๋ ๋ชจ๋ ์์ ์ถ๋ ฅ
for (let i=0; i<superheroes.length; i++){
console.log(superheroes[i]);
}
//forEachํจ์ ์ฌ์ฉ
superheroes.forEach(hero => {
console.log(hero);
});
- forEach ํจ์์ ํ๋ผ๋ฏธํฐ๋ก ๊ฐ ์์์ ๋ํ์ฌ ์ฒ๋ฆฌํ๊ณ ์ถ์ ์ฝ๋๋ฅผ ํจ์๋ก ๋ฃ์ด์ค
- ์ฝ๋ฐฑํจ์: ํจ์ํํ์ ํ๋ผ๋ฏธํฐ๋ฅผ ์ ๋ฌํ๋ ๊ฒ
map
: ๋ฐฐ์ด ์์ ๊ฐ ์์๋ฅผ ๋ณํํ ๋ ์ฌ์ฉ, ์๋ก์ด ๋ฐฐ์ด์ด ๋ง๋ค์ด์ง
const array = [1,2,3,4,5,6,7,8];
const squared = [];
//for๋ฌธ ์ด์ฉ
for (let i=0; i<array.length; i++){
squared.push(array[i]*array[i]);
}
console.log(squared);
//forEach ์ด์ฉ
array.forEach(n => {
squared.push(n*n);
});
console.log(squared);
//map์ด์ฉ
const square= n => n*n;
const squared = array.map(square);
console.log(squared);
- mapํจ์์ ํ๋ผ๋ฏธํฐ๋ก ๋ณํ๋ฅผ ์ฃผ๋ ํจ์ ์ ๋ฌ(๋ณํํจ์)
indexOf
: ์ํ๋ ํญ๋ชฉ์ด ๋ช ๋ฒ์งธ ์์์ธ์ง ์ฐพ์์ฃผ๋ ํจ์
const superheroes = ['์์ด์ธ๋งจ', '์บกํด ์๋ฉ๋ฆฌ์นด', 'ํ ๋ฅด', '๋ฅํฐ ์คํธ๋ ์ธ์ง'];
const index=superheroes.indexOf('ํ ๋ฅด');
console.log(index);
- index๊ฐ์ 0๋ถํฐ ์์
findIndex
: ๋ฐฐ์ด ์์ ์๋ ๊ฐ์ด ๊ฐ์ฒด์ด๊ฑฐ๋, ๋ฐฐ์ด์ธ ๊ฒฝ์ฐ ๋ช ๋ฒ์งธ ์ธ์ง ์ฐพ์์ฃผ๋ ํจ์(์กฐ๊ฑด์ ๋ฐํํ๋ ํจ์ ๋ฃ์ด ์ฐพ์)
const todos = [
{
id: 1,
text: '์๋ฐ์คํฌ๋ฆฝํธ ์
๋ฌธ',
done: true
},
{
id: 2,
text: 'ํจ์ ๋ฐฐ์ฐ๊ธฐ',
done: true
},
{
id: 3,
text: '๊ฐ์ฒด์ ๋ฐฐ์ด ๋ฐฐ์ฐ๊ธฐ',
done: true
},
{
id: 4,
text: '๋ฐฐ์ด ๋ด์ฅํจ์ ๋ฐฐ์ฐ๊ธฐ',
done: false
}
];
const index=todos.findIndex(todo => todo.id===3);
console.log(index);
find
: findIndex๋ ๋น์ท, ์ฐพ์๋ธ ๊ฐ ์์ฒด๋ฅผ ๋ฐํ
const todos = [
{
id: 1,
text: '์๋ฐ์คํฌ๋ฆฝํธ ์
๋ฌธ',
done: true
},
{
id: 2,
text: 'ํจ์ ๋ฐฐ์ฐ๊ธฐ',
done: true
},
{
id: 3,
text: '๊ฐ์ฒด์ ๋ฐฐ์ด ๋ฐฐ์ฐ๊ธฐ',
done: true
},
{
id: 4,
text: '๋ฐฐ์ด ๋ด์ฅํจ์ ๋ฐฐ์ฐ๊ธฐ',
done: false
}
];
const todo = todos.find(todo => todo.id === 3);
console.log(todo);
//๊ฒฐ๊ณผ๊ฐ: {id: 3, text: "๊ฐ์ฒด์ ๋ฐฐ์ด ๋ฐฐ์ฐ๊ธฐ", done: true}
filter
: ๋ฐฐ์ด์์ ํน์ ์กฐ๊ฑด์ ๋ง์กฑํ๋ ๊ฐ๋ค๋ง ๋ฐ๋ก ์ถ์ถํ์ฌ ์๋ก์ด ๋ฐฐ์ด ๋ง๋ฆ
const todos = [
{
id: 1,
text: '์๋ฐ์คํฌ๋ฆฝํธ ์
๋ฌธ',
done: true
},
{
id: 2,
text: 'ํจ์ ๋ฐฐ์ฐ๊ธฐ',
done: true
},
{
id: 3,
text: '๊ฐ์ฒด์ ๋ฐฐ์ด ๋ฐฐ์ฐ๊ธฐ',
done: true
},
{
id: 4,
text: '๋ฐฐ์ด ๋ด์ฅํจ์ ๋ฐฐ์ฐ๊ธฐ',
done: false
}
];
const tasksNotDone = todos.filter(todo => todo.done === false);
console.log(tasksNotDone);
/*
๊ฒฐ๊ณผ๊ฐ
[
{
id: 4,
text: '๋ฐฐ์ด ๋ด์ฅ ํจ์ ๋ฐฐ์ฐ๊ธฐ',
done: false
}
];
*/
- filterํจ์์ ๋ฃ๋ ํ๋ผ๋ฏธํฐ: ์กฐ๊ฑด์ ๊ฒ์ฌํ๋ ํจ์(๊ฐ ์์์ ๊ฐ ๋ฐ์์ด)
splice
: ๋ฐฐ์ด์์ ํน์ ํญ๋ชฉ ์ ๊ฑฐ
const numbers = [10, 20, 30, 40];
const index = numbers.indexOf(30);
numbers.splice(index, 1);
console.log(numbers);
//๊ฒฐ๊ณผ๊ฐ
//[10, 20, 40]
- ์ฒซ ๋ฒ์งธ ํ๋ผ๋ฏธํฐ๋ ์ด๋ค ์ธ๋ฑ์ค๋ฅผ ์ง์ธ์ง ์๋ฏธ, ๋ ๋ฒ์งธ ํ๋ผ๋ฏธํฐ๋ ๊ทธ ์ธ๋ฑ์ค๋ก ๋ถํฐ ๋ช ๊ฐ๋ฅผ ์ง์ธ์ง ์๋ฏธ
slice
: ๋ฐฐ์ด์ ์๋ผ๋ผ ๋ ์ฌ์ฉ, ๊ธฐ์กด์ ๋ฐฐ์ด์ ๊ฑด๋ค์ด์ง ์์
const numbers=[10,20,30,40];
const sliced=numbers.slice(0,2); //0๋ถํฐ ์์ํด์ 2์ ๊น์ง
console.log(sliced); //[10,20]
console.log(numbers); //[10,20,30,40]
- ์ฒซ ๋ฒ์งธ ํ๋ผ๋ฏธํฐ๋ ์ด๋์๋ถํฐ ์๋ฅผ์ง, ๋ ๋ฒ์งธ ํ๋ผ๋ฏธํฐ๋ ์ด๋๊น์ง ์๋ฅผ์ง ์๋ฏธ
shift์ pop
shift: ์ฒซ ๋ฒ์งธ ์์๋ฅผ ๋ฐฐ์ด์์ ์ถ์ถ (์ถ์ถํ๋ ๊ณผ์ ์์ ๋ฐฐ์ด์์ ํด๋น ์์ ์ฌ๋ผ์ง)
const numbers = [10, 20, 30, 40];
const value = numbers.shift();
console.log(value);
console.log(numbers);
/*
10
[20, 30, 40]
*/
pop: push์ ๋ฐ๋ ๊ฐ๋ , push๋ ๋ฐฐ์ด์ ๋งจ ๋ง์ง๋ง์ ์ ํญ๋ชฉ ์ถ๊ฐ, pop์ ๋งจ ๋ง์ง๋ง ํญ๋ชฉ์ ์ถ์ถ
const numbers = [10, 20, 30, 40];
const value = numbers.pop();
console.log(value);
console.log(numbers);
/*
40
[10, 20, 30]
*/
unshift
: ๋ฐฐ์ด์ ๋งจ ์์ ์ ์์ ์ถ๊ฐ
const numbers = [10, 20, 30, 40];
numbers.unshift(5);
console.log(numbers);
//[5, 10, 20, 30, 40]
concat
: ์ฌ๋ฌ ๊ฐ์ ๋ฐฐ์ด์ ํ๋์ ๋ฐฐ์ด๋ก ํฉ์นจ
const arr1 = [1, 2, 3];
const arr2 = [4, 5, 6];
const concated = arr1.concat(arr2);
console.log(concated);
//[1, 2, 3, 4, 5, 6];
- arr1๊ณผ arr2์ ๋ณํ๋ฅผ ์ฃผ์ง ์์(์ฆ, ์๋ก์ด ๋ฐฐ์ด์ ๋ง๋ค์ด๋)
join
: ๋ฐฐ์ด ์์ ๊ฐ๋ค์ ๋ฌธ์์ด ํํ๋ก ํฉ์นจ
const array = [1, 2, 3, 4, 5];
console.log(array.join()); // 1,2,3,4,5
console.log(array.join(' ')); // 1 2 3 4 5
console.log(array.join(', ')); // 1, 2, 3, 4, 5
reduce
: ๋ฐฐ์ด์ ๊ฐ ์์๋ฅผ ์ํํ๋ฉฐ ์ฝ๋ฐฑํจ์์ ์คํ ๊ฐ์ ๋์ ํ์ฌ ํ๋์ ๊ฒฐ๊ณผ๊ฐ ๋ฐํ
//์ฃผ์ด์ง ๋ฐฐ์ด์ ๋ํ ์ดํฉ ๊ตฌํ๊ธฐ
const numbers = [1, 2, 3, 4, 5];
let sum = 0;
//forEach ์ฌ์ฉ
numbers.forEach(n => {
sum += n;
});
console.log(sum);
//reduce ์ฌ์ฉ
let sum = array.reduce((accumulator, current) = > accumulator+current,0);
console.log(sum);
- ์ฒซ ๋ฒ์งธ ํ๋ผ๋ฏธํฐ๋ accumulator์ current๋ฅผ ํ๋ผ๋ฏธํฐ๋ก ๊ฐ์ ธ์ ๊ฒฐ๊ณผ๋ฅผ ๋ฐํํ๋ ์ฝ๋ฐฑํจ์
- ๋ ๋ฒ์งธ ํ๋ผ๋ฏธํฐ๋ reduceํจ์์์ ์ฌ์ฉ ํ ์ด๊ธฐ๊ฐ
'ํ๋ก ํธ์๋ > React' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
| useEffect์ useMemo (0) | 2022.11.23 |
|---|---|
| ์ปดํฌ๋ํธ ๋ ๋๋ง (0) | 2022.11.23 |
| ๋ฆฌ์กํธ์ ๋ฐฐ์ด (0) | 2022.11.17 |
| useRef (0) | 2022.11.16 |
| ๋ฆฌ์กํธ Hooks (0) | 2022.11.15 |