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

时间:2016-11-13 07:36:16

标签: javascript lodash

在这里,我有一个名为mainArray的主阵列。在这个数组中我添加了多个用户列表。它可以增加。我可以从这个mainarray创建许多组,例如group1,group2等等。我的要求是,我想过滤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"
}];

组,

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);

Concatinated group id,

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

从主数组中过滤对象

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

3 个答案:

答案 0 :(得分:2)

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)




答案 1 :(得分:1)

创建组中现有项目的索引,然后使用它来过滤原始数组。

ES6(因为你正在使用React):

>>> a = [[1,2,3],[1,2,3],3,5,6]
>>> [y for x in a for y in (x if isinstance(x, list) else [x])]
[1, 2, 3, 1, 2, 3, 3, 5, 6]

使用lodash:

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);
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);

答案 2 :(得分:0)

您可以使用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>
&#13;
&#13;
&#13;