https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Global_Objects/Array/some
const array = [1, 2, 3, 4, 5];
const even = (val) => val % 2 === 0;
console.log(array.some(even));
// true
const found = (val) => val % 6 === 0;
console.log(array.some(found));
// false
https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Global_Objects/Array/every
const array = [1, 2, 3, 4, 5];
const isBelowThreshold = (val) => val < 6;
console.log(array.every(isBelowThreshold));
// true
const isBelowThreshold = (val) => val < 4;
console.log(array.every(isBelowThreshold));
// false
https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Global_Objects/Array/find
const array = [1, 2, 3, 4, 5];
const found = array.find(val => val > 3);
console.log(found);
// 4
const found2 = array.find(val => val > 5);
console.log(found2);
// undefined
https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Global_Objects/Array/findIndex
const array = [1, 2, 3, 4, 5];
const found = array.findIndex(val => val > 3);
console.log(found);
// 3
const found2 = array.find(val => val > 5);
console.log(found2);
[Javascript]배열에서 해당 조건을 만족하는 결과를 얻고 싶을 때 (every, some, find, findIndex) (0) | 2022.05.12 |
---|