gpt4 book ai didi

javascript - 基于属性的多个数组的交集

转载 作者:行者123 更新时间:2023-11-30 13:53:57 24 4
gpt4 key购买 nike

我想根据一个公共(public)属性找到多个对象数组的公共(public)元素。此外,如果一个元素出现不止一次,我希望生成的数组能够反射(reflect)它在所有数组中出现的次数。

我尝试了以下方法:

var arr = [
[
{ name: 'kiwi', value: 12 },
{ name: 'apple', value: 5 },
{ name: 'apple', value: 12 },
{ name: 'pizza', value: 33 },
{ name: 'pizza', value: 24 },
{ name: 'fish', value: 5 },
{ name: 'milk', value: 5 },
{ name: 'banana', value: 7 },
{ name: 'orange', value: 11 },
],
[
{ name: 'taco', value: 23 },
{ name: 'pizza', value: 78 },
{ name: 'apple', value: 12 },
{ name: 'pizza', value: 33 },
{ name: 'pizza', value: 24 },
{ name: 'fish', value: 5 },
{ name: 'pie', value: 1 },
{ name: 'cake', value: 3 },
{ name: 'banana', value: 7 },
{ name: 'beef', value: 123 },
{ name: 'lime', value: 72 },
{ name: 'pizza', value: 34 },
],
[
{ name: 'apple', value: 12 },
{ name: 'pizza', value: 33 },
{ name: 'pizza', value: 24 },
{ name: 'pizza', value: 23 },
{ name: 'fish', value: 5 },
{ name: 'banana', value: 7 },
{ name: 'banana', value: 77 },
]
];

function findArraysWithCommonName(arr) {
let arrays = [...arr];
var result = arrays.shift().reduce(function(res, v) {
if (arrays.every(function(a) {
return (a.filter(function(e) {
return e.name === v.name
}).length > 0);
})) res.push(v);
return res;
}, []);
return result;
}

console.log(findArraysWithCommonName(arr))

我得到的结果是:

[
{name: "apple", value: 5},
{name: "apple", value: 12},
{name: "pizza", value: 33},
{name: "pizza", value: 24},
{name: "fish", value: 5},
{name: "banana", value: 7}
]

我希望输出是:

[
{name: "apple", value: 12},
{name: "pizza", value: 33},
{name: "pizza", value: 24},
{name: "fish", value: 5},
{name: "banana", value: 7}
]

[
{name: "apple", value: 5},
{name: "pizza", value: 33},
{name: "pizza", value: 24},
{name: "fish", value: 5},
{name: "banana", value: 7}
]

最佳答案

一种方法是构建一个映射,将对象与其在数组中的“计数”相关联(即对象在 arr 中出现的次数)。

这可以通过 .reduce() 完成,您可以通过 JSON.stringify(obj) 将每个对象序列化为一个字符串 - 这个字符串是相应的对象形状和状态,用作来标识映射中此表单的对象。 key 用于查询和更新映射的“计数”值,对于在 arr 中遇到的每个对象。

构建映射后,根据“计数”值大于 1 的条目过滤映射条目。

最后对于任何过滤的条目,通过.map()反序列化这些条目的相应键,以获得在原始arr中出现更多对象的数组.

这种方法可以实现为:

var arr=[[{name:'kiwi',value:12},{name:'apple',value:5},{name:'apple',value:12},{name:'pizza',value:33},{name:'pizza',value:24},{name:'fish',value:5},{name:'milk',value:5},{name:'banana',value:7},{name:'orange',value:11}],[{name:'taco',value:23},{name:'pizza',value:78},{name:'apple',value:12},{name:'pizza',value:33},{name:'pizza',value:24},{name:'fish',value:5},{name:'pie',value:1},{name:'cake',value:3},{name:'banana',value:7},{name:'beef',value:123},{name:'lime',value:72},{name:'pizza',value:34}],[{name:'apple',value:12},{name:'pizza',value:33},{name:'pizza',value:24},{name:'pizza',value:23},{name:'fish',value:5},{name:'banana',value:7},{name:'banana',value:77}]];


/* Flatten array heirachy */
const flatArr = arr.flat();

/* Obtain a count mapping for each object's occourance in flatArr */
const mapObjectToCount = flatArr.reduce((map, item) => {

const key = JSON.stringify(item);
const count = (map[key] ? map[key] : 0) + 1;

return { ...map, [ key ] : count };
}, {})

/* Get key/value pair of the prior mapping, filter the objects by
those that occour more that one time, and obtain the original object
by parsing the key */
const result = Object.entries(mapObjectToCount)
.filter(([json, count]) => count > 1)
.map(([json]) => JSON.parse(json));

console.log(result)

关于javascript - 基于属性的多个数组的交集,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57650251/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com