AngularJS 在第一个数组中找到 object,但第二个数组中没有
AngularJS find object in first array which is not presented in second
假设我有两个 objects:
第一个:
[
{
id: "123",
title: "123",
options: []
},
{
id: "456",
title: "456",
options: [
{
id: "0123",
title: "0123",
options: []
}
]
},
{
id: "789",
title: "789",
options: []
},
]
第二个
[
{
id: "123",
title: "123",
options: []
},
{
id: "789",
title: "789",
options: []
},
]
正如您在第二个数组中看到的那样,我遗漏了这部分:
{
编号:“456”,
标题:“456”,
选项: [
{
编号:“0123”,
标题:“0123”,
选项: []
}
]
}
在 angular 中迭代和查找缺失的元素如何才是正确和更好的?
假设第一个数组命名为 first
,第二个数组命名为 second
。现在先对它们进行排序:
function comp(a, b){
if(a.id < b.id) return -1;
if(a.id > b.id) return 1;
return 0;
}
first.sort(comp);
second.sort(comp);
然后遍历它们以找到缺失的元素:
var missing = {};
for(var i = 0, j = 0; i < first.length; ++i){
if(first[i].id == second[j].id){
j++;
continue;
}
missing.push(first[i]);
}
missing
数组现在包含在第一个数组中但不在第二个数组中的对象。
请注意,我没有使用 AngularJS;很简单 Javascript.
你可以这样做
<div ng-app>
<div ng-controller="MyCtrl">{{availableGroups}}
</div>
</div>
js代码
function MyCtrl ($scope) {
$scope.groups = [
{
id: "123",
title: "123",
options: []
},
{
id: "456",
title: "456",
options: [
{
id: "0123",
title: "0123",
options: []
}
]
},
{
id: "789",
title: "789",
options: []
},
];
$scope.assignedGroups = [
{
id: "123",
title: "123",
options: []
},
{
id: "789",
title: "789",
options: []
},
];
$scope.availableGroups = (function () {
var assignedGroupsIds = {};
var groupsIds = {};
var result = [];
$scope.assignedGroups.forEach(function (el, i) {
assignedGroupsIds[el.id] = $scope.assignedGroups[i];
});
$scope.groups.forEach(function (el, i) {
groupsIds[el.id] = $scope.groups[i];
});
for (var i in groupsIds) {
if (!assignedGroupsIds.hasOwnProperty(i)) {
result.push(groupsIds[i]);
}
}
return result;
}());
}
这里正在工作jsFiddle
谢谢
假设我有两个 objects:
第一个:
[
{
id: "123",
title: "123",
options: []
},
{
id: "456",
title: "456",
options: [
{
id: "0123",
title: "0123",
options: []
}
]
},
{
id: "789",
title: "789",
options: []
},
]
第二个
[
{
id: "123",
title: "123",
options: []
},
{
id: "789",
title: "789",
options: []
},
]
正如您在第二个数组中看到的那样,我遗漏了这部分:
{ 编号:“456”, 标题:“456”, 选项: [ { 编号:“0123”, 标题:“0123”, 选项: [] } ] }
在 angular 中迭代和查找缺失的元素如何才是正确和更好的?
假设第一个数组命名为 first
,第二个数组命名为 second
。现在先对它们进行排序:
function comp(a, b){
if(a.id < b.id) return -1;
if(a.id > b.id) return 1;
return 0;
}
first.sort(comp);
second.sort(comp);
然后遍历它们以找到缺失的元素:
var missing = {};
for(var i = 0, j = 0; i < first.length; ++i){
if(first[i].id == second[j].id){
j++;
continue;
}
missing.push(first[i]);
}
missing
数组现在包含在第一个数组中但不在第二个数组中的对象。
请注意,我没有使用 AngularJS;很简单 Javascript.
你可以这样做
<div ng-app>
<div ng-controller="MyCtrl">{{availableGroups}}
</div>
</div>
js代码
function MyCtrl ($scope) {
$scope.groups = [
{
id: "123",
title: "123",
options: []
},
{
id: "456",
title: "456",
options: [
{
id: "0123",
title: "0123",
options: []
}
]
},
{
id: "789",
title: "789",
options: []
},
];
$scope.assignedGroups = [
{
id: "123",
title: "123",
options: []
},
{
id: "789",
title: "789",
options: []
},
];
$scope.availableGroups = (function () {
var assignedGroupsIds = {};
var groupsIds = {};
var result = [];
$scope.assignedGroups.forEach(function (el, i) {
assignedGroupsIds[el.id] = $scope.assignedGroups[i];
});
$scope.groups.forEach(function (el, i) {
groupsIds[el.id] = $scope.groups[i];
});
for (var i in groupsIds) {
if (!assignedGroupsIds.hasOwnProperty(i)) {
result.push(groupsIds[i]);
}
}
return result;
}());
}
这里正在工作jsFiddle
谢谢