NodeJS / Javascript在不同数组中查找具有相同属性的对象

Posted

技术标签:

【中文标题】NodeJS / Javascript在不同数组中查找具有相同属性的对象【英文标题】:NodeJS/Javascript Finding Objects with same Property in different Array 【发布时间】:2018-11-03 15:41:21 【问题描述】:

我在 NodeJS 中做一些事情,需要比较不同数组中的对象的重复属性。

例子:

Items = ["id": "10", "name": "milk","id":"7", "name": "banana","id":"47", "name": "cheese","id":"6", "name":"carrot"]
Fridge = ["id":"19", "name": "cheese","id": "10", "name": "milk","id":"43","name": "egg", "id":"6", "name":"not a carrot"]

我想获取数组中具有包含在两个数组中的属性的所有对象。 (糟糕的解释,见示例)

在这个例子中,我想得到

Output = ["id": "10", "name": "milk","id":"6", "name":"not a carrot"]

因为只有这 2 个 ID 存在于两个输入数组中。

我该怎么做?

【问题讨论】:

【参考方案1】:

您可以将includes 方法与filter 结合使用。

Items = ["id": "10", "name": "milk","id":"7", "name": "banana","id":"47", "name": "cheese","id":"6", "name":"carrot"]
Fridge = ["id":"19", "name": "cheese","id": "10", "name": "milk","id":"43","name": "egg", "id":"6", "name":"not a carrot"]

result = Fridge.filter(item => Items.map(f => f.id).includes(item.id));
console.log(result);

【讨论】:

【参考方案2】:

您可以使用 lodash.js 轻松实现这一点,如下所示:

如果您只想检查 id:

    let Items = ["id": "10", "name": "milk","id":"7", "name": "banana","id":"47", "name": "cheese","id":"6", "name":"carrot"]
let Fridge = ["id":"19", "name": "cheese","id": "10", "name": "milk","id":"43","name": "egg", "id":"6", "name":"not a carrot"]

let result = _.intersectionBy(Fridge, Items, 'id');

console.log(result);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.10/lodash.min.js"></script>

或者如果你想比较整个对象:

let Items = ["id": "10", "name": "milk","id":"7", "name": "banana","id":"47", "name": "cheese","id":"6", "name":"carrot"]
let Fridge = ["id":"19", "name": "cheese","id": "10", "name": "milk","id":"43","name": "egg", "id":"6", "name":"not a carrot"]

let result = _.intersectionWith(Items, Fridge, _.isEqual);

console.log(result);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.10/lodash.min.js"></script>

`

【讨论】:

【参考方案3】:

另一种方法可以用于查找具有 n 个对象数组作为输入的重复项:

var Items = ["id": "10", "name": "milk","id":"7", "name": "banana","id":"47", "name": "cheese","id":"6", "name":"carrot"];
var Fridge = ["id":"19", "name": "cheese","id": "10", "name": "milk","id":"43","name": "egg", "id":"6", "name":"not a carrot"];

var mergedArrray = Items.concat(Fridge); //merge all
//array1 = array1.concat(array2, array3, array4, ..., arrayN);    


var duplicateIds = [];
var uniqueIds = [];

//The idea is look for the id in uniqueIds, if exist push into duplicateIds
//if not push into uniqueIds 
mergedArrray.map((obj,v)=>
(uniqueIds.indexOf(obj.id) == -1)?uniqueIds.push(obj.id):duplicateIds.push(obj.id);
)

console.log(duplicateIds);
console.log(uniqueIds);

此解决方案不返回重复的数组,只返回引用。

【讨论】:

以上是关于NodeJS / Javascript在不同数组中查找具有相同属性的对象的主要内容,如果未能解决你的问题,请参考以下文章

遇到 Async/Await Promise 问题 - Javascript/Nodejs

在 NodeJS 中解析 JSON 对象数组

Javascript Nodejs值未推入数组[重复]

Javascript(NodeJs) - 将数组和对象转换为 mysql 中的批量插入(Sequelize)

javascript初识 NodeJS

Javascript,Nodejs:在文件中搜索特定的单词字符串