如何返回两个对象的差异?
$scope.a = [
{name: "Lunchmeat"},
{name: "Apple"},
{name: "Bread"},
{name: "Milk"},
{name: "Mustard"},
{name: "Cheese"}
];
$scope.b = [
{name: "Bread"},
{name: "Milk"},
{name: "Mustard"},
{name: "Cheese"}
];
$.grep($scope.a, function (el) {
if ($.inArray($scope.b,el) == -1) console.log(el);//not working
});
我正在寻找的输出是Object [{name: "Lunchmeat"},{name: "Apple"}]
,因为这就是区别。
有人可以帮忙吗?
最佳答案
我正在使用underscore.js库及其功能区别:
"difference_.difference(array, *others) Similar to without, but returns the values from array that are not present in the other arrays.
_.difference([1, 2, 3, 4, 5], [5, 2, 10]); => [1, 3, 4]"
在你的情况下,它是:
$scope.someVar =_.difference($scope.a,$scope.b);
关于javascript - Angular - 返回不工作对象的差异,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32009701/