我知道这有用(返回true)
var arr1 = [1, 'a', 2, 'b', 3];
var arr2 = [1, 2, 3];
var isSuperset = arr2.every(function (val) { return arr1.indexOf(val) >= 0; });
但是说array1由对象组成,我想根据对象的某个属性检查array2:
var object1 = {name:'one'}
var object2 = {name:'two'}
var object3 = {name:'three'}
var arr1 = [object1,object2,object3];
var arr2 = ['one','two'];
var isSuperset = arr2.every(function (val) { return arr1.indexOf(val) >= 0; });
如何确保每个函数都对name属性进行检查?
答案 0 :(得分:7)
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);