Answers for "check if an array contains all elements in another array in javascript"

9

javascript check if elements of one array are in another

const found = arr1.some(r=> arr2.includes(r))
Posted by: Guest on March-26-2020
0

javascript check if array is subset of another

let superSet = ['B', 'C', 'A', 'D'];
let subSet = ['D', 'C'];
let mixedSet = new Set([...superSet, ...subSet]);
let isSubset = mixedSet.size == superSet.length
Posted by: Guest on January-18-2021
0

check if array contain the all element javascript

const myArray: number[] = [2, 4, 6, 8, 10, 12, 14, 16];
const elements: number[] = [4, 8, 12, 16];

function containsAll(arr: number[]) {
  return (
    arr.includes(elements[0]) &&
    arr.includes(elements[1]) &&
    arr.includes(elements[2]) &&
    arr.includes(elements[3])
  );
}
console.log(containsAll(myArray));

or you could use the following line:

function c2(arr: number[]) {
  return elements.every((val: number) => arr.includes(val));
}
Posted by: Guest on November-29-2020

Code answers related to "check if an array contains all elements in another array in javascript"

Code answers related to "Javascript"

Browse Popular Code Answers by Language