[英]How to make an intersection between two arrays of objects
我正在尝试在两个对象数组之间进行交集。
const list1 = [
{
name: "skinType",
keys: [
{
id: "oily",
label: "Oily"
},
{
id: "dry",
label: "Dry"
}
]
},
{
name: "finish",
keys: [
{
id: "oily",
label: "Oily"
},
{
id: "dry",
label: "Dry"
},
{
id: "matte",
label: "Matte"
},
{
id: "natural",
label: "Natural"
},
{
id: "radiant",
label: "Radiant / Glow"
}
]
},
{
name: "texture",
keys: [
{
id: "matte",
labal: "Matte"
}
]
}
];
const list2 = [
{
name: "skinType",
keys: [
{
id: "oily",
label: "Oily"
},
{
id: "dry",
label: "Dry"
},
{
id: "gandac",
label: "mazga"
}
]
},
{
name: "finish",
keys: [
{
id: "oily",
label: "Oily"
}
]
}
];
我提出了一个解决方案,但它只能根据对象的名称密钥进行交集。 现在我需要根据keys数组中的id进行交集。
const intersection = (list1, list2) => {
return list2.filter(drp => list1.some(rect => rect.name === drp.name));
};
const result = intersection(react, drupal);
预期结果:
[
{
name: "skinType",
keys: [
{
id: "oily",
label: "Oily"
},
{
id: "dry",
label: "Dry"
}
]
},
{
name: "finish",
keys: [
{
id: "oily",
label: "Oily"
}
]
}
]
const react = [ { name: "skinType", keys: [ { id: "oily", label: "Oily" }, { id: "dry", label: "Dry" } ] }, { name: "finish", keys: [ { id: "oily", label: "Oily" }, { id: "dry", label: "Dry" }, { id: "matte", label: "Matte" }, { id: "natural", label: "Natural" }, { id: "radiant", label: "Radiant / Glow" } ] }, { name: "texture", keys: [ { id: "matte", labal: "Matte" } ] } ]; const drupal = [ { name: "skinType", keys: [ { id: "oily", label: "Oily" }, { id: "dry", label: "Dry" }, { id: "gandac", label: "mazga" } ] }, { name: "finish", keys: [ { id: "oily", label: "Oily" } ] } ]; var result = []; for(var item1 of react) for(var item2 of drupal) if(item1.name == item2.name){ var keys = item2.keys.filter(x => item1.keys.some(y => y.id === x.id)); result.push({name: item1.name, keys}) break; } console.log(result);
你关心重复吗?
const array1 = [1,2,3,4]; const array2 = [5,6,7,8]; const union = (a1, a2) => [...a1, ...a2]; console.log(union(array1, array2));
如果你不想重复
const array1 = [1,2,3,4,5]; const array2 = [4,5,6,7,8]; const distinct = (array) => array ? array.reduce((results, item) => { if (!results.some((i) => i === item)) { results.push(item); } return results; }, []) : array; const union = (a1, a2) => distinct([...a1, ...a2]); console.log(union(array1, array2));
另一种方法是通过首先将数组减少到映射(通过key.id
标准),然后通过Object.values()
提取映射的值来避免联合结果中的重复值,以获得作为数组的并集价值观
中间映射阶段背后的想法是确保结果联合中的值是key.id
唯一的。
在代码中的解决方案可以如下所示:
const react=[{name:"skinType",keys:[{id:"oily",label:"Oily"},{id:"dry",label:"Dry"}]},{name:"finish",keys:[{id:"oily",label:"Oily"},{id:"dry",label:"Dry"},{id:"matte",label:"Matte"},{id:"natural",label:"Natural"},{id:"radiant",label:"Radiant / Glow"}]},{name:"texture",keys:[{id:"matte",labal:"Matte"}]}];const drupal=[{name:"skinType",keys:[{id:"oily",label:"Oily"},{id:"dry",label:"Dry"},{id:"gandac",label:"mazga"}]},{name:"finish",keys:[{id:"oily",label:"Oily"}]}]; /* Gather both arrays into a combined array via concat() and reduce() the result to obtain the union. The reduce() produces a mapping, and we obtain the union array via Object.values() to extract the map's values as an array */ const union = Object.values([].concat(drupal, react).reduce((map, item) => { /* For each item of the keys sub-array, update the map with value "key" at key "key.id" given that key.id is the union criteria */ item.keys.forEach(key => { map[key.id] = key; }) return map; }, {})) console.log(union);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.