How to Check If an Array Is a Subset of Another Array in JavaScript

Checking if an array is a subset of another array (but checking against a property)

var object1 = {name: 'one'};
var object2 = {name: 'two'};
var object3 = {name: 'three'};

var arr1 = [object1,object2,object3];
var arr2 = ['one','two'];

// solution
var names = arr1.map(function(obj) {
return obj.name;
});

var isSuperset = arr2.every(function(val) {
return names.indexOf(val) >= 0;
});

alert(isSuperset);

Check if one array is contained in another array

You can do this using Array.prototype.some. This will run the provided function against all the items in an array, and return true if the function returns true for any of them. The following will return true if any items from array are not contained in otherArray, which you can use to determine if one array is fully contained in the other:

return !array.some(function(item) {
return otherArray.indexOf(item) === -1;
});

However, this is not the most elegant solution. The logic can be summed up as:

not any items from array not in other array

Which has far too many negatives. We can instead use Array.prototype.every, which is very similar except it returns true only if all items in an array return true for the provided function. The below is equivalent to what we had before:

return array.every(function(item) {
return otherArray.indexOf(item) !== -1;
});

Except that can be summed up as:

all items in array in other array

Finally, we can implement this as an additional prototype function. Note that the second parameter for every is optional, and sets what this refers to in the function when provided. If we did not pass it in, we would not be able to refer to the this from the outside scope.

Array.prototype.contains = function(array) {
return array.every(function(item) {
return this.indexOf(item) !== -1;
}, this);
}

This can now be used as a one liner in our code:

['a', 'b', 'c'].contains(['a', 'b']) // returns true

If you are able to use ECMAScipt 6, you can use arrow functions to make this a true one-liner.

return array.every(item => otherArray.indexOf(item) !== -1);

JEST - check if objects array is a subset of another array

I've never tried this myself, but wouldn't it work to just say:

expect(users).toEqual(
expect.arrayContaining(subset)
)

Check if an array of object is subset of another array of object

Thats because you're not doing .name when checking .indexOf

var object1 = {
name: 'one',
psno: '34'
};
var object2 = {
name: 'two',
psno: '34'
};
var object3 = {
name: 'three',
psno: '345'
};
var arr1 = [object1, object2, object3];
var arr2 = [object1, object2];
// solution
var names = arr1.map(function(obj) {
return obj.name;
});
var isSuperset = arr2.every(function(val) {
//Ive changed this line!
return names.indexOf(val.name) >= 0;
});
alert(isSuperset);

JS Fiddle here:
https://jsfiddle.net/waqmafsa/

Check if array is a subset of another array and list all the differences at the end with an assert failure using js/chai/mocha

Couldn't find any built in method. For now, Used For loops to iterate through each element to compare and saved the mismatches to an output array.



Related Topics



Leave a reply



Submit