我有这个JSON数据:
vm.userListData = [{
"listId": 1,
"permission": "READ"
}, {
"listId": 2,
"permission": "WRITE"
}, {
"listId": 2,
"permission": "READ"
}, {
"listId": 3,
"permission": "READ"
}, {
"listId": 3,
"permission": "WRITE"
}, {
"listId": 4,
"permission": "WRITE"
}, {
"listId": 5,
"permission": "WRITE"
}]
这一个:
vm.userComplementaryList = [{
"listId": 1,
"confidentiality": "PUBLIC",
"listName": "List name here..1",
"permission": "WRITE"
}, {
"listId": 2,
"confidentiality": "PUBLIC",
"listName": "List name here..2",
"permission": "READ"
}, {
"listId": 3,
"confidentiality": "CONFIDENTIAL",
"listName": "List name here..3",
"permission": "WRITE"
}, {
"listId": 4,
"confidentiality": "CONFIDENTIAL",
"listName": "List name here..4",
"permission": "WRITE"
}, {
"listId": 5,
"confidentiality": "CONFIDENTIAL",
"listName": "List name here..5",
"permission": "READ"
}]
使用这两个JSON数据,我必须过滤并获取唯一值并将它们推送到数组和重复的值(listId和permission),将它们推送到其他数组中。 我这样做了:
vm.listForGrid = [];
vm.listForDropDown = [];
(function(){
for(var i = 0; i < vm.userComplementaryList.length; i++) {
for(var j = 0; j < vm.userListData.length; j++) {
if( (vm.userComplementaryList[i].listId == vm.userListData[j].listId) && (vm.userComplementaryList[i].permission == vm.userListData[j].permission) ) {
vm.listForGrid.push(vm.userComplementaryList[i]);
}
else {
vm.listForDropDown.push(vm.userComplementaryList[i]);
}
}
}
})();
vm.listForGrid
没问题,但vm.listForDropDown
值多于同一个值,我必须break
循环。
重复的值与listId和两个数组中的权限相同
谢谢!
答案 0 :(得分:3)
如果给出了权限,您仍可以使用哈希表作为参考。然后你只需要一个没有嵌套循环的循环来对项目进行排序。
var vm = {},
permissions = {};
vm.userListData = [{ "listId": 1, "permission": "READ" }, { "listId": 2, "permission": "WRITE" }, { "listId": 2, "permission": "READ" }, { "listId": 3, "permission": "READ" }, { "listId": 3, "permission": "WRITE" }, { "listId": 4, "permission": "WRITE" }, { "listId": 5, "permission": "WRITE" }];
vm.userComplementaryList = [{ "listId": 1, "confidentiality": "PUBLIC", "listName": "List name here..1", "permission": "WRITE" }, { "listId": 2, "confidentiality": "PUBLIC", "listName": "List name here..2", "permission": "READ" }, { "listId": 3, "confidentiality": "CONFIDENTIAL", "listName": "List name here..3", "permission": "WRITE" }, { "listId": 4, "confidentiality": "CONFIDENTIAL", "listName": "List name here..4", "permission": "WRITE" }, { "listId": 5, "confidentiality": "CONFIDENTIAL", "listName": "List name here..5", "permission": "READ" }];
vm.listForDropDown = [];
vm.userListData.forEach(function (p) {
permissions[p.listId] = permissions[p.listId] || {};
permissions[p.listId][p.permission] = true;
});
vm.listForGrid = vm.userComplementaryList.filter(function (a) {
if (permissions[a.listId] && permissions[a.listId][a.permission]) {
return true;
}
vm.listForDropDown.push(a);
});
console.log(vm.listForGrid);
console.log(vm.listForDropDown);
.as-console-wrapper { max-height: 100% !important; top: 0; }
答案 1 :(得分:2)
这是使用array.some
的分拣机版本View has already been added to the window manager.
at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:243)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:5942)
at java.lang.reflect.Method.invoke(Method.java)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
答案 2 :(得分:0)
我解决了这个问题:
(function(){
for(var i = 0; i < vm.userComplementaryList.length; i++) {
var found = false;
for(var j = 0; j < vm.userListData.length; j++) {
if( (vm.userComplementaryList[i].listId == vm.userListData[j].listId) && (vm.userComplementaryList[i].permission == vm.userListData[j].permission) ) {
found = true;
break;
}
}
if(found) {
vm.listForGrid.push(vm.userComplementaryList[i]);
}
else {
vm.listForDropDown.push(vm.userComplementaryList[i]);
}
}
})();