반응형

자바스크립트에서 자주 사용하는 배열 함수에 대해서 정리하려고 합니다.
filter
filter(callback)
배열에서 callback 함수가 true 인 값만 배열로 반환합니다.
const hero = ["superman", "batman", "pororo"]
const result = hero.filter(person => person === "superman");
console.log(result); ["superman"]
superman 값이 있는 배열이 반환됩니다.
every
every(callback)
배열 안의 값들이 callback 함수 조건을 통과하는지 판별합니다. true, false값을 반환합니다.
const hero = ["superman", "batman", "pororo"]
const result = hero.every((person => person.length > 3));
console.log(result); // true
배열 안의 요소들이 모두 글자수가 3개 이상이여서 true를 반환합니다.
some
some(callback)
배열 안의 값들이 callback 함수 조건을 적어도 하나라도 통과하는지 확인합니다. 하나라도 통과하면 true, 모두 통과하지 않으면 false를 반환합니다.
const hero = ["superman", "batman", "pororo"]
const result3 = hero.some(person => person === "ionman");
console.log(result); // false
배열안의 요소에 ionman이 없어서 false를 반환합니다.
728x90
반응형