JavaScript: Check If Array Has All Elements From Another Array - Designcise

PHOTO EMBED

Tue Dec 14 2021 19:59:19 GMT+0000 (Coordinated Universal Time)

Saved by @arielvol

const arr1 = [ 1, 2, 3 ];
const arr2 = [ 3, 5, 4, 2, 7, 0, 1, 10 ];

let hasAllElems = true;

for (let i = 0; i < arr1.length; i++){
    if (arr2.indexOf(arr1[i]) === -1) {
        hasAllElems = false;
        break;
    }
}

console.log(hasAllElems); // output: true
content_copyCOPY

https://www.designcise.com/web/tutorial/how-to-check-if-an-array-contains-all-elements-of-another-array-in-javascript