2017-09-22 116 views
0

我有這樣的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和權限相同的值!

+0

'break'當你發現listid相同分揀機版本。 –

+0

把你的代碼放在plunkr –

回答

2

這是使用array.some

vm.userComplementaryList.forEach(function(vCom) { 
    vm.userListData.some(function(vUser) { 
     return (vCom.listId == vUser.listId && vCom.permission == vUser.permission); 
    }) ? vm.listForGrid.push(vCom) : vm.listForDropDown.push(vCom); 
}) 
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; }

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]); 
     } 
    } 
})(); 
+0

你仍然需要一個嵌套循環來找到相同的元素。 –