배열의 요소를 하나씩 살펴보면서 일치하는 요소만 모아서 새로운 배열을 리턴하는 메서드이다.
const devices = [
{name: 'GalaxyNote', brand: 'Samsung'},
{name: 'MacbookPro', brand: 'Apple'},
{name: 'Gram', brand: 'LG'},
{name: 'SurfacePro', brand: 'Microsoft'},
{name: 'ZenBook', brand: 'Asus'},
{name: 'MacbookAir', brand: 'Apple'},
];
const apples = devices.filter((element, index, array) => {
return element.brand === 'Apple';
});
console.log(apples); // (2) [{name: "MacbookPro", brand: "Apple"}, {name: "MacbookAir", brand: "Apple"}]
filter
메서드와 비슷하게 동작하지만 배열의 요소들을 반복하는 중에 리턴하는 조건과 일치하는 가장 첫번째 요소를 리턴하고 반복을 종료하는 메서드이다.
const devices = [
{name: 'GalaxyNote', brand: 'Samsung'},
{name: 'MacbookPro', brand: 'Apple'},
{name: 'Gram', brand: 'LG'},
{name: 'SurfacePro', brand: 'Microsoft'},
{name: 'ZenBook', brand: 'Asus'},
{name: 'MacbookAir', brand: 'Apple'},
];
const myLaptop = devices.find((element, index, array) => {
console.log(index); // 콘솔에는 0, 1, 2까지만 출력됨.
return element.name === 'Gram';
});
console.log(myLaptop); // {name: "Gram", brand: "LG"}