我有一个带有嵌套类别对象的对象数组(请参见下文)。我想删除重复的对象(基于“ objectID”),并仅使用嵌套数量较多的“ hierarchicalCategories”键值对的对象创建新数组
const objectsArray = [
{
"objectID": 1234,
"hierarchicalCategories": {
"lvl0": "Women's",
"lvl1": "Women's > Shirts",
"lvl2": "Women's > Shirts > Tees"
}
},
{
"objectID": 5678,
"hierarchicalCategories": {
"lvl0": "Men's"
}
},
{
"objectID": 1234,
"hierarchicalCategories": {
"lvl0": "Women's"
}
},
{
"objectID": 5678,
"hierarchicalCategories": {
"lvl0": "Men's",
"lvl1": "Men's > Shoes"
}
}
]
因此预期结果将如下所示:最终数组将过滤重复项并保留每个对象的一个实例...“ objectID”:1234实例的“ hierarchicalCategories”最大为“ lvl2”,而“ objectID” :5678个实例的“ hierarchicalCategories”到“ lvl1”
const newArray = [
{
"objectID": 1234,
"hierarchicalCategories": {
"lvl0": "Women's",
"lvl1": "Women's > Shirts",
"lvl2": "Women's > Shirts > Tees"
}
},
{
"objectID": 5678,
"hierarchicalCategories": {
"lvl0": "Men's",
"lvl1": "Men's > Shoes"
}
}
]
我具有此功能,该功能适用于基于过滤重复的objectID的新数组,但是我不确定如何创建逻辑以使对象具有更多的“ hierarchicalCategories”键值对。
const newArray = Array.from(new Set(objectsArray.map(a => a.objectID)))
.map(objectID => {
return objectsArray.find(a => a.objectID === objectID)
})
真的很感谢我如何解决此问题的任何指导。谢谢!
答案 0 :(得分:3)
使用.sort()
和.filter()
从最高级别的级别到最低级别进行排序并进行过滤,以便新数组中仅最高级别的级别。
const objectsArray = [
{
"objectID": 1234,
"hierarchicalCategories": {
"lvl0": "Women's",
"lvl1": "Women's > Shirts",
"lvl2": "Women's > Shirts > Tees"
}
},
{
"objectID": 5678,
"hierarchicalCategories": {
"lvl0": "Men's"
}
},
{
"objectID": 1234,
"hierarchicalCategories": {
"lvl0": "Women's"
}
},
{
"objectID": 5678,
"hierarchicalCategories": {
"lvl0": "Men's",
"lvl1": "Men's > Shoes"
}
}
]
const newArray = objectsArray.sort((a, b) => Object.keys(b.hierarchicalCategories).length - Object.keys(a.hierarchicalCategories).length).filter((v, i, a) => i === a.findIndex(e => e.objectID === v.objectID));
console.log(newArray);
.as-console-wrapper {
max-height: 100% !important;
top: 0;
}
答案 1 :(得分:2)
您可以使用Map
并将类别分配给同一组。
const
objectsArray = [{ objectID: 1234, hierarchicalCategories: { lvl0: "Women's", lvl1: "Women's > Shirts", lvl2: "Women's > Shirts > Tees" } }, { objectID: 5678, hierarchicalCategories: { lvl0: "Men's" } }, { objectID: 1234, hierarchicalCategories: { lvl0: "Women's" } }, { objectID: 5678, hierarchicalCategories: { lvl0: "Men's", lvl1: "Men's > Shoes" } }],
result = Array.from(objectsArray
.reduce((m, o) => {
if (m.has(o.objectID)) {
Object.assign(m.get(o.objectID).hierarchicalCategories, o.hierarchicalCategories);
return m;
}
return m.set(o.objectID, o);
}, new Map)
.values()
);
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
一种直接收集类别并从收集的部分中构建新对象的较短方法。
const
objectsArray = [{ objectID: 1234, hierarchicalCategories: { lvl0: "Women's", lvl1: "Women's > Shirts", lvl2: "Women's > Shirts > Tees" } }, { objectID: 5678, hierarchicalCategories: { lvl0: "Men's" } }, { objectID: 1234, hierarchicalCategories: { lvl0: "Women's" } }, { objectID: 5678, hierarchicalCategories: { lvl0: "Men's", lvl1: "Men's > Shoes" } }],
result = Array.from(
objectsArray.reduce((m, { objectID: id, hierarchicalCategories: o }) =>
m.set(id, Object.assign((m.get(id) || {}), o)), new Map),
([objectID, hierarchicalCategories]) => ({ objectID, hierarchicalCategories })
);
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
答案 2 :(得分:1)
const shouldAdd = (item, target) => {
if (!target[item.objectID]) return true;
if (target[item.objectID] && !target[item.objectID].hierarchicalCategories && item.hierarchicalCategories) return true;
if (target[item.objectID] && target[item.objectID].hierarchicalCategories && item.hierarchicalCategories && Object.keys(target[item.objectID].hierarchicalCategories).length < Object.keys(item.hierarchicalCategories).length) return true;
}
let newObjectsArray = {}
objectsArray.forEach((obj) => {
if (shouldAdd(obj, newObjectsArray)) {
newObjectsArray[obj.objectID] = obj
}
})
console.log(Object.values(newObjectsArray)); //Object.values(newObjectsArray) should contain what you want
答案 3 :(得分:1)
const objectsArray = [
{
"objectID": 1234,
"hierarchicalCategories": {
"lvl0": "Women's",
"lvl1": "Women's > Shirts",
"lvl2": "Women's > Shirts > Tees"
}
},
{
"objectID": 5678,
"hierarchicalCategories": {
"lvl0": "Men's"
}
},
{
"objectID": 1234,
"hierarchicalCategories": {
"lvl0": "Women's"
}
},
{
"objectID": 5678,
"hierarchicalCategories": {
"lvl0": "Men's",
"lvl1": "Men's > Shoes"
}
}
]
var result_arr = objectsArray.reduce((acc, curr) => {
const existing_obj = acc.find(item => item.objectID === curr.objectID);
if (existing_obj) {
if (Object.keys(curr.hierarchicalCategories).length > Object.keys(existing_obj.hierarchicalCategories).length)
existing_obj.hierarchicalCategories = {...curr.hierarchicalCategories };
} else {
acc.push(curr);
}
return acc;
}, []);
console.log(result_arr)