728x90

✨ 직접 작성한 function을 주입하여, 각 elements를 test 할 수 있는 helper function

(e.g., arr.every(char => char === 'a')) 왼쪽처럼 test할 function을 주입하고, arr의 값이 1개 있다고 가정할 때, 그 arr의 값이 'a'와 동일하다면 true가 반환될 것이고, 아니라면 false가 반환될 것이다.
Palindrome(회문 구조) Algorithm에서 유용하게 사용하였다.

💻Example Code

const arr = [ 'a' ];

console.log( arr.every((char, index) => char === arr[index]));

실행 결과(값이 같기 때문에 true)

😋 Palindrome과 같이 전체 array를 처음부터 또는 마지막부터 하나씩 검사하기에 좋다.
더 활용된 내용은 palindrome algorithm을 참고하면 됩니다.

👉 자세한 내용은 https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/every

'JavaScript > Built-in Method etc.' 카테고리의 다른 글

String.prototype.toLowerCase() & toUpperCase()  (0) 2019.12.28
Array.prototype.includes()  (0) 2019.12.26
Array.prototype.reduce()  (0) 2019.12.24
Array.prototype.join()  (0) 2019.12.24
Array.prototype.reverse()  (0) 2019.12.24

+ Recent posts