简体   繁体   English

查找对象数组的所有常见元素-Javascript

[英]Find all common elements of an array of objects - Javascript

I have data structured as follows: 我的数据结构如下:

var Items = [{"Name":"type1","Options":[1,2,5]},{"Name":"type2","Options":[1,2]},{"Name":"type1","Options":[2,5]}];

I am fairly new to javascript but I'm trying to figure out the common options associated with each name type. 我对javascript相当陌生,但是我想弄清楚与每种名称类型相关的常见选项。

The number of elements in the Items array is arbitrary. Items数组中的元素数是任意的。 So I could have 40 for example. 例如,我可以有40个。

My expected output for the above data would be 我对上述数据的预期输出为

CommonOptions = [{"Name":"type1","Options":[2,5]},{"Name":"type2","Options":[1,2]}];

because 2 and 5 are common to all elements with name type1 and 1,2 are common to all elements with elements with name type2. 因为2和5对于名称为type1的所有元素都是公共的,而1,2对于名称为type2的所有元素都是公共的。 I don't know how to access the data correctly. 我不知道如何正确访问数据。

Here is where I am up to. 这是我要做的。 If someone could steer me in the right direction I'd really appreciate it. 如果有人可以引导我朝正确的方向前进,我将非常感激。

 var Items = [{ "Name": "type1", "Options": [1, 2, 5] }, { "Name": "type2", "Options": [1, 2] }, { "Name": "type1", "Options": [2, 5] }]; //Define the Common Options Array as the first options and associated name var CommonOptions = []; CommonOptions.push(Items[0]); //the first item is already in the common options array so start at 1 for (var i = 1, iLen = Items.length - 1; i < iLen; i++) { for (var j = 0, cLen = CommonOptions.length; j < cLen; j++) { //add all unique by name options to common options array if (CommonOptions[j].Name.indexOf(Items[i].Name) === -1) { //item name is not in the common options array //add item to common options array CommonOptions.push(Items[i]); } else { //item name is in the common options array // if it is in the common options array then check each Option in the Option array against the common options of that name //CommonOptions[j].Options.indexOf(Items[i].Options)===-1 } } } console.log(CommonOptions); 

You could use a hash table for same named objects and filter Options for common elements. 您可以将哈希表用于相同的命名对象,并将过滤器Options用于公共元素。

 var items = [{ Name: "type1", Options: [1, 2, 5] }, { Name: "type2", Options: [1, 2] }, { Name: "type1", Options: [2, 5] }], hash = Object.create(null), common = items.reduce(function (r, o) { if (hash[o.Name]) { hash[o.Name].Options = hash[o.Name].Options.filter(function (v) { return o.Options.indexOf(v) !== -1; }); } else { hash[o.Name] = { Name: o.Name, Options: o.Options.slice() }; r.push(hash[o.Name]); } return r; }, []); console.log(common); 
 .as-console-wrapper { max-height: 100% !important; top: 0; } 

You just need to keep separate the current state of the array for each name, and further filter it as that name is encountered. 您只需要为每个名称保留数组的当前状态,并在遇到该名称时对其进行进一步过滤。

 const Items = [{"Name":"type1","Options":[1,2,5]},{"Name":"type2","Options":[1,2]},{"Name":"type1","Options":[2,5]}]; const m = Items.reduce((m, o) => { const a = m.get(o.Name); return m.set(o.Name, a ? a.filter(n => o.Options.includes(n)) : o.Options); }, new Map()); const res = Array.from(m.entries(), ([Name, Options]) => ({Name, Options})); console.log(res); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM