Javascript
배열 객체(5)
GYChoi
2022. 2. 6. 21:26

배열 메서드
filter() : 조건에 만족하는 배열 요소 검색(반환:배열)
{
const arrNum = [100, 200, 300, 400, 500];
const result = arrNum.filter(el => el === 300);
const arrNum2 = [100, 200, 300, 400, 500];
const result2 = arrNum2.filter(el => el === 600);
const arrNum3 = [100, 200, 300, 400, 500];
const result3 = arrNum3.filter(el => el >= 300);
}
기본값 | 메서드 | 결괏값 |
[100, 200, 300, 400, 500] | .filter(el => el === 300) | 300 |
[100, 200, 300, 400, 500] | .filter(el => el === 600) | |
[100, 200, 300, 400, 500] | .filter(el => el >= 300) | 300,400,500 |
map() : 배열 요소를 추출하여 새로운 배열로 만듦(반환:배열)
{
const arrNum = [100, 200, 300, 400, 500];
const result = arrNum.map(el => el);
const arrNum2 = [100, 200, 300, 400, 500];
const result2 = arrNum2.map(el => el + 'J');
const arrNum3 = [100, 200, 300, 400, 500];
const result3 = arrNum3.map(el => el + 100);
const arrNum4 = [{a:100}, {a:200}, {a:300}];
const result4 = arrNum4.map(el => el.a);
}
기본값 | 메서드 | 결괏값 |
[100, 200, 300, 400, 500] | .map(el => el) | 100,200,300,400,500 |
[100, 200, 300, 400, 500] | .map(el => el + 'J') | 100J,200J,300J,400J,500J |
[100, 200, 300, 400, 500] | .map(el => el + 100) | 200,300,400,500,600 |
[{a:100}, {a:200}, {a:300}] | .map(el => el.a) | 100,200,300 |