Skip to content Skip to sidebar Skip to footer

See If An Array Contains All The Number From Other Nested Arrays With Javascript?

I have an array of arrays called winingNumbers. I need to test other arrays (userOneNumers and userTwoNumers) to see if they contain all of the numbers from any of winingNumbers's

Solution 1:

You could take a set and check against with previously check same length of arrays.

functioncheck(a, b) {
    var bb = newSet(b);
    return a.some(aa => aa.length === b.length && aa.every(aaa => bb.has(aaa)));
}


const userOneNumers = [1, 2, 4, 5];
const userTwoNumers = [1, 2, 3];
const winingNumbers = [[1, 2, 3], [4, 5, 6]];

console.log(check(winingNumbers, userOneNumers)); // falseconsole.log(check(winingNumbers, userTwoNumers)); // true

Edit after edit of the quesion, without length check, just check an inner array against the given values.

functioncheck(a, b) {
    var bb = newSet(b);
    return a.some(aa => aa.every(aaa => bb.has(aaa)));
}


const userOneNumers = [1, 2, 4, 5];
const userTwoNumers = [1, 2, 3, 6];
const winingNumbers = [[1, 2, 3], [4, 5, 6]];

console.log(check(winingNumbers, userOneNumers)); // falseconsole.log(check(winingNumbers, userTwoNumers)); // true

Post a Comment for "See If An Array Contains All The Number From Other Nested Arrays With Javascript?"