我有這樣的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和權限),將它們推送到其他數組中。 我做到了這一點:
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
都OK,但vm.listForDropDown
值,曾多次相同的價值,我必須break
循環。
重複值的兩個陣列中
謝謝配合listId和權限相同的值!
'break'當你發現listid相同分揀機版本。 –
把你的代碼放在plunkr –