JavaScript新手,对JS对象知之甚少。我希望得到两个JS对象的区别。
a=[{"month":1,"year":2015},{"month":2,"year":2015},{"month":3,"year":2015},{"month":4,"year":2015},{"month":5,"year":2015}];
b=[{"month":1,"year":2015},{"month":2,"year":2015},{"month":5,"year":2015}];
我梦寐以求的结果应该是
result=[{"month":3,"year":2015},{"month":4,"year":2015}];
我使用JSON.stringify
获得了上述格式。
答案 0 :(得分:4)
下面应该适合你,只需在数组上使用filter
。
var a=[{"month":1,"year":2015},{"month":2,"year":2015},{"month":3,"year":2015},{"month":4,"year":2015},{"month":5,"year":2015}];
var b=[{"month":1,"year":2015},{"month":2,"year":2015},{"month":5,"year":2015}];
var diff = a.filter(function(a){
return b.filter(function(b){
return b.month == a.month && b.year == a.year
}).length == 0
});
console.log(diff);