[英]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.