简体   繁体   English

如何从JavaScript中的键过滤对象数组?

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

Here, I have one main array called mainArray . 在这里,我有一个名为mainArray主数组。 In this array I added multiple users list. 在此数组中,我添加了多个用户列表。 It can be increased. 可以增加。 I can create many group from this mainarray , like group1, group2 and so on. 我可以从这个mainarray创建许多组,例如group1,group2等。 My requirement is, i want to filter objects from mainArray , which haven't added in the groups. 我的要求是,我要从mainArray过滤对象,这些对象尚未添加到组中。

Array main, 数组主

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"
}];

Groups, 组,

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"
}];

Joined group ids, 已加入的网上论坛ID,

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

The output which i want to be, 我想成为的输出

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

my javascript code, 我的JavaScript代码,

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

Concatinated group ids, 组合的组ID,

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

Filter objects from main array, 从主数组过滤对象,

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

indexOf will not work for searching inside arrays. indexOf不适用于在数组内部搜索。 Use .findIndex 使用.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) 

Save userIds in joinedGroup 保存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) 

Create an index of the existing items in the groups, then use it to filter the original array. 创建组中现有项目的索引,然后使用它来过滤原始数组。

ES6 (since you're using React): 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); 

Using lodash: 使用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> 

You could make use of differenceWith method and get this done. 您可以使用differenceWith方法并完成此操作。

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

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

Here is the working solution. 这是有效的解决方案。

Documentation: _.differenceWith 文档: _.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