JS array in array

김재경·2022년 3월 7일
0

js 하다보면 배열 안에 배열이 있는지 확인해야할 경우가 더러 있다.
다음과 같이 간단하게 확인할 수 있다.

let arrA = [1, 2, 3, 4];
let arrB = [1, 2, 3, 4, 5];
let arrC = [2, 3, 4, 5]

let compareA_B = arrA.every((element) => arrB.includes(element));
let compareA_C = arrA.every((element) => arrC.includes(element));

console.log(compareA_B); // true
console.log(compareA_C); // false

참고 https://appdividend.com/2019/03/14/javascript-array-includes-example-array-prototype-includes-tutorial/

profile
프런트앤드 개발자

0개의 댓글