簡體   English   中英

如何從JavaScript中的鍵過濾對象數組?

[英]How to filter array of objects from keys in javascript?

在這里,我有一個名為mainArray主數組。 在此數組中,我添加了多個用戶列表。 可以增加。 我可以從這個mainarray創建許多組,例如group1,group2等。 我的要求是,我要從mainArray過濾對象,這些對象尚未添加到組中。

數組主

var mainArray = [{
  userId: "M000001",
  name: "Jhon",
  companyId: "C0000021"
}, {
  userId: "M000002",
  name: "Leon",
  companyId: "C0000022"
}, {
  userId: "M000003",
  name: "Thomas",
  companyId: "C0000023"
}, {
  userId: "M000004",
  name: "Sean",
  companyId: "C0000024"
}, {
  userId: "M000005",
  name: "Paul",
  companyId: "C0000025"
}, {
  userId: "M000006",
  name: "Roldan",
  companyId: "C0000026"
}, {
  userId: "M000007",
  name: "Mike",
  companyId: "C0000027"
}, {
  userId: "M000008",
  name: "Mia",
  companyId: "C0000028"
}];

組,

var group1 = [{
  userId: "M000004",
  name: "Sean",
  companyId: "C0000024"
}, {
  userId: "M000005",
  name: "Paul",
  companyId: "C0000025"
}];

var group2 = [{
  userId: "M000002",
  name: "Leon",
  companyId: "C0000022"
}, {
  userId: "M000003",
  name: "Thomas",
  companyId: "C0000023"
}, {
  userId: "M000001",
  name: "John",
  companyId: "C0000021"
}];

已加入的網上論壇ID,

var joinedGroupIds = ["M000004", "M000005", "M000002", "M000003", "M000001"];

我想成為的輸出

var result = [{
  {
    userId: "M000006",
    name: "Roldan",
    companyId: "C0000026"
  }, {
    userId: "M000007",
    name: "Mike",
    companyId: "C0000027"
  }, {
    userId: "M000008",
    name: "Mia",
    companyId: "C0000028"
  }
}];

我的JavaScript代碼,

var joinGroup, joinedGroupIds = [];
joinGroup = group1.concat(group2);

組合的組ID,

joinedGroupIds.map(function(el){
  joinedGroupIds.push(el.userId);
});

從主數組過濾對象,

var result = mainArray.filter(function(item) {
  if (joinedGroupIds.indexOf(item.userId) !== -1) return item;
});

indexOf不適用於在數組內部搜索。 使用.findIndex

var result = mainArray.filter(function(x) {
  return joinedGroup.findIndex(function(y) {
    return y.userId === x.userId
  }) === -1
})

 var mainArray = [{ userId: "M000001", name: "Jhon", companyId: "C0000021" }, { userId: "M000002", name: "Leon", companyId: "C0000022" }, { userId: "M000003", name: "Thomas", companyId: "C0000023" }, { userId: "M000004", name: "Sean", companyId: "C0000024" }, { userId: "M000005", name: "Paul", companyId: "C0000025" }, { userId: "M000006", name: "Roldan", companyId: "C0000026" }, { userId: "M000007", name: "Mike", companyId: "C0000027" }, { userId: "M000008", name: "Mia", companyId: "C0000028" }]; var group1 = [{ userId: "M000004", name: "Sean", companyId: "C0000024" }, { userId: "M000005", name: "Paul", companyId: "C0000025" }]; var group2 = [{ userId: "M000002", name: "Leon", companyId: "C0000022" }, { userId: "M000003", name: "Thomas", companyId: "C0000023" }, { userId: "M000001", name: "John", companyId: "C0000021" }]; var joinedGroup = group1.concat(group2); var result = mainArray.filter(function(x) { return joinedGroup.findIndex(function(y) { return y.userId === x.userId }) === -1 }) console.log(result) 

保存userIds在joinedGroup

var joinedGroup = [];
group1.forEach(x => joinedGroup.push(x.userId))
group2.forEach(x => joinedGroup.push(x.userId))

var result = mainArray.filter(function(x) {
  return joinedGroup.indexOf(x.userId) === -1
})

 var mainArray = [{ userId: "M000001", name: "Jhon", companyId: "C0000021" }, { userId: "M000002", name: "Leon", companyId: "C0000022" }, { userId: "M000003", name: "Thomas", companyId: "C0000023" }, { userId: "M000004", name: "Sean", companyId: "C0000024" }, { userId: "M000005", name: "Paul", companyId: "C0000025" }, { userId: "M000006", name: "Roldan", companyId: "C0000026" }, { userId: "M000007", name: "Mike", companyId: "C0000027" }, { userId: "M000008", name: "Mia", companyId: "C0000028" }]; var group1 = [{ userId: "M000004", name: "Sean", companyId: "C0000024" }, { userId: "M000005", name: "Paul", companyId: "C0000025" }]; var group2 = [{ userId: "M000002", name: "Leon", companyId: "C0000022" }, { userId: "M000003", name: "Thomas", companyId: "C0000023" }, { userId: "M000001", name: "John", companyId: "C0000021" }]; var joinedGroup = []; group1.forEach(x => joinedGroup.push(x.userId)) group2.forEach(x => joinedGroup.push(x.userId)) var result = mainArray.filter(function(x) { return joinedGroup.indexOf(x.userId) === -1 }) console.log(result) 

創建組中現有項目的索引,然后使用它來過濾原始數組。

ES6(因為您使用的是React):

 const filterByGroups = (arr, ...groups) => { // create a Set of existing userId in the groups const exitingItems = new Set( [].concat([], ...groups).map(({ userId }) => userId) ); // filter from the array all items that their userId exists in the Set return arr.filter(({ userId }) => !exitingItems.has(userId)); }; var mainArray = [{ userId: "M000001", name: "Jhon", companyId: "C0000021" }, { userId: "M000002", name: "Leon", companyId: "C0000022" }, { userId: "M000003", name: "Thomas", companyId: "C0000023" }, { userId: "M000004", name: "Sean", companyId: "C0000024" }, { userId: "M000005", name: "Paul", companyId: "C0000025" }, { userId: "M000006", name: "Roldan", companyId: "C0000026" }, { userId: "M000007", name: "Mike", companyId: "C0000027" }, { userId: "M000008", name: "Mia", companyId: "C0000028" }]; var group1 = [{ userId: "M000004", name: "Sean", companyId: "C0000024" }, { userId: "M000005", name: "Paul", companyId: "C0000025" }]; var group2 = [{ userId: "M000002", name: "Leon", companyId: "C0000022" }, { userId: "M000003", name: "Thomas", companyId: "C0000023" }, { userId: "M000001", name: "John", companyId: "C0000021" }]; const result = filterByGroups(mainArray, group1, group2); console.log(result); 

使用lodash:

 function filterByGroups(arr) { var existingItems = _([].slice.call(arguments, 1)) .flatten() .keyBy('userId') .value(); return arr.filter(function(item) { return !existingItems[item.userId]; }); } var mainArray = [{ userId: "M000001", name: "Jhon", companyId: "C0000021" }, { userId: "M000002", name: "Leon", companyId: "C0000022" }, { userId: "M000003", name: "Thomas", companyId: "C0000023" }, { userId: "M000004", name: "Sean", companyId: "C0000024" }, { userId: "M000005", name: "Paul", companyId: "C0000025" }, { userId: "M000006", name: "Roldan", companyId: "C0000026" }, { userId: "M000007", name: "Mike", companyId: "C0000027" }, { userId: "M000008", name: "Mia", companyId: "C0000028" }]; var group1 = [{ userId: "M000004", name: "Sean", companyId: "C0000024" }, { userId: "M000005", name: "Paul", companyId: "C0000025" }]; var group2 = [{ userId: "M000002", name: "Leon", companyId: "C0000022" }, { userId: "M000003", name: "Thomas", companyId: "C0000023" }, { userId: "M000001", name: "John", companyId: "C0000021" }]; var result = filterByGroups(mainArray, group1, group2); console.log(result); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.16.6/lodash.min.js"></script> 

您可以使用differenceWith方法並完成此操作。

let comparator = (a, b) => JSON.stringify(a) === JSON.stringify(b);

let result = _.differenceWith(mainArray, group1.concat(group2), comparator);

這是有效的解決方案。

文檔: _.differenceWith

 var mainArray = [{ userId: "M000001", name: "John", companyId: "C0000021" }, { userId: "M000002", name: "Leon", companyId: "C0000022" }, { userId: "M000003", name: "Thomas", companyId: "C0000023" }, { userId: "M000004", name: "Sean", companyId: "C0000024" }, { userId: "M000005", name: "Paul", companyId: "C0000025" }, { userId: "M000006", name: "Roldan", companyId: "C0000026" }, { userId: "M000007", name: "Mike", companyId: "C0000027" }, { userId: "M000008", name: "Mia", companyId: "C0000028" }]; var group1 = [{ userId: "M000004", name: "Sean", companyId: "C0000024" }, { userId: "M000005", name: "Paul", companyId: "C0000025" }]; var group2 = [{ userId: "M000002", name: "Leon", companyId: "C0000022" }, { userId: "M000003", name: "Thomas", companyId: "C0000023" }, { userId: "M000001", name: "John", companyId: "C0000021" }]; let comparator = (a, b) => JSON.stringify(a) === JSON.stringify(b); let result = _.differenceWith(mainArray, group1.concat(group2), comparator); console.log(result); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.16.6/lodash.min.js"></script> 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM