遍历具有 ID 数组中的 ID 的 objects 和 return objects 数组
Go through array of objects and return objects that have Ids from an array of Ids
我想使用 Lodash 的全部功能,但我不明白为什么我不能用 Lodash 执行此操作。
我有一个包含 38 个 objects 的数组,每个数组都有 ID。我还有另一个 ID 数组。我需要将它们映射到 objects 的数组,并让它们在 objects.
的新数组中可用
我正在寻找这样的解决方案:
var workOrder.model.Lands = [{
ID: "123",
LandArea: "",
LandAreaDescription: "something",
LandAreaNo: 0,
Value: null,
Text: null,
Status: true,
GeoJsonData: null,
GeoJsonCenter: null,
Blocks: Array[3]
},...];
var landIDs = [1, 2, 3, 4];
_.each(workOrder.model.Lands, function(land){
_.each(landIDs, function(id){
console.log(id);
if(land.ID === id){
workOrder.selectedLandArea.push(land);
}
});
});
这行不通,实际上 returns 土地 collections 中的所有物品都是我的。解决这些 objects 问题的最佳解决方案是什么?
没有JQUERY
提前致谢
更新
添加了变量以使其更加清晰
相关代码如下(假设objs
为object数组,ids
为id数组)
_.filter(objs, function(obj) {
return _.includes(ids, obj.ID);
});
从逻辑上讲,您是在根据另一个集合中包含的成员键过滤一个集合。
我想使用 Lodash 的全部功能,但我不明白为什么我不能用 Lodash 执行此操作。
我有一个包含 38 个 objects 的数组,每个数组都有 ID。我还有另一个 ID 数组。我需要将它们映射到 objects 的数组,并让它们在 objects.
的新数组中可用我正在寻找这样的解决方案:
var workOrder.model.Lands = [{
ID: "123",
LandArea: "",
LandAreaDescription: "something",
LandAreaNo: 0,
Value: null,
Text: null,
Status: true,
GeoJsonData: null,
GeoJsonCenter: null,
Blocks: Array[3]
},...];
var landIDs = [1, 2, 3, 4];
_.each(workOrder.model.Lands, function(land){
_.each(landIDs, function(id){
console.log(id);
if(land.ID === id){
workOrder.selectedLandArea.push(land);
}
});
});
这行不通,实际上 returns 土地 collections 中的所有物品都是我的。解决这些 objects 问题的最佳解决方案是什么?
没有JQUERY
提前致谢
更新 添加了变量以使其更加清晰
相关代码如下(假设objs
为object数组,ids
为id数组)
_.filter(objs, function(obj) {
return _.includes(ids, obj.ID);
});
从逻辑上讲,您是在根据另一个集合中包含的成员键过滤一个集合。