const array1 = [[1, 2, 3], [1,3,4], [1,2,5]];
let b = []
let c = [2,3]
array1.forEach(e => {
c.some(r => {
if(e.includes(r))
b.push(e)
})
})
console.log(b)
got the result as [ [ 1, 2, 3 ], [ 1, 2, 3 ], [ 1, 3, 4 ], [ 1, 3, 5 ] ]
but the expected result is [ [ 1, 2, 3 ]]
1
Filter the original array, and use Array.every()
and Array.includes()
to check that all items in c
appear in the subArray
:
const array1 = [[1,2,3], [1,3,4], [1,3,5], [1,2,5]];
const c = [2,3]
const result = array1.filter(subArray =>
c.every(n => subArray.includes(n))
)
console.log(result)
You can also use the new Set methods to check if c
is a subset of subArray
:
const array1 = [[1,2,3], [1,3,4], [1,3,5], [1,2,5]];
const c = new Set([2,3])
const result = array1.filter(subArray =>
c.isSubsetOf(new Set(subArray))
)
console.log(result)
4