如何合并数组内对象中的元素并返回具有更新数据的唯一对象
How to merge elements in objects inside of array and returned unique objects with updated data
我的问题是如何将对象数组中的元素和 return 数组中的元素与唯一对象合并。我正在尝试使用 reduce,但看起来我遗漏了一些东西,是时候寻求提示了。
所以你怎么看我需要合并布尔元素,其中 false 是默认值,true 是新数据,return 基于名称的唯一对象。谢谢你的帮助。
const arr = [
{
code: 'test1',
name: 'TEST1',
url: '/test1',
elements: {
option1: true,
option2: false,
option3: false,
option4: false,
option5: false
}
},
{
code: 'test1',
name: 'TEST1',
url: '/test1',
elements: {
option1: false,
option2: false,
option3: true,
option4: false,
option5: false
}
},
{
code: 'test1',
name: 'TEST1',
url: '/test1',
elements: {
option1: false,
option2: false,
option3: false,
option4: true,
option5: false
}
},
{
code: 'test2',
name: 'TEST2',
url: '/test2',
elements: {
option1: false,
option2: false,
option3: true,
option4: false,
option5: false
}
},
]
输出应该是:
const arr = [
{
code: 'test1',
name: 'TEST1',
url: '/test1',
elements: {
option1: true,
option2: false,
option3: true,
option4: true,
option5: false
}
},
{
code: 'test2',
name: 'TEST2',
url: '/test2',
elements: {
option1: false,
option2: false,
option3: true,
option4: false,
option5: false
}
},
]
假设 elements
是这里唯一实际变化的东西,并且您不能从 false
切换回 true
(因为看起来就像你想要的那样),你需要迭代元素中的 key-value 对并将其设置为 originalValue || newValue
,所以像这样:
const arr = [
{
code: 'test1',
name: 'TEST1',
url: '/test1',
elements: {
option1: true,
option2: false,
option3: false,
option4: false,
option5: false
}
},
{
code: 'test1',
name: 'TEST1',
url: '/test1',
elements: {
option1: false,
option2: false,
option3: true,
option4: false,
option5: false
}
},
{
code: 'test1',
name: 'TEST1',
url: '/test1',
elements: {
option1: false,
option2: false,
option3: false,
option4: true,
option5: false
}
},
{
code: 'test2',
name: 'TEST2',
url: '/test2',
elements: {
option1: false,
option2: false,
option3: true,
option4: false,
option5: false
}
},
].reduce((carry, current) => {
const existing = carry.find(item => item.code === current.code);
if (!existing) {
carry.push(current);
} else {
Object.entries(current.elements).forEach(([k, v]) => {
existing.elements[k] = existing.elements[k] || current.elements[k];
});
}
return carry;
}, []);
console.log(arr);
我的问题是如何将对象数组中的元素和 return 数组中的元素与唯一对象合并。我正在尝试使用 reduce,但看起来我遗漏了一些东西,是时候寻求提示了。 所以你怎么看我需要合并布尔元素,其中 false 是默认值,true 是新数据,return 基于名称的唯一对象。谢谢你的帮助。
const arr = [
{
code: 'test1',
name: 'TEST1',
url: '/test1',
elements: {
option1: true,
option2: false,
option3: false,
option4: false,
option5: false
}
},
{
code: 'test1',
name: 'TEST1',
url: '/test1',
elements: {
option1: false,
option2: false,
option3: true,
option4: false,
option5: false
}
},
{
code: 'test1',
name: 'TEST1',
url: '/test1',
elements: {
option1: false,
option2: false,
option3: false,
option4: true,
option5: false
}
},
{
code: 'test2',
name: 'TEST2',
url: '/test2',
elements: {
option1: false,
option2: false,
option3: true,
option4: false,
option5: false
}
},
]
输出应该是:
const arr = [
{
code: 'test1',
name: 'TEST1',
url: '/test1',
elements: {
option1: true,
option2: false,
option3: true,
option4: true,
option5: false
}
},
{
code: 'test2',
name: 'TEST2',
url: '/test2',
elements: {
option1: false,
option2: false,
option3: true,
option4: false,
option5: false
}
},
]
假设 elements
是这里唯一实际变化的东西,并且您不能从 false
切换回 true
(因为看起来就像你想要的那样),你需要迭代元素中的 key-value 对并将其设置为 originalValue || newValue
,所以像这样:
const arr = [
{
code: 'test1',
name: 'TEST1',
url: '/test1',
elements: {
option1: true,
option2: false,
option3: false,
option4: false,
option5: false
}
},
{
code: 'test1',
name: 'TEST1',
url: '/test1',
elements: {
option1: false,
option2: false,
option3: true,
option4: false,
option5: false
}
},
{
code: 'test1',
name: 'TEST1',
url: '/test1',
elements: {
option1: false,
option2: false,
option3: false,
option4: true,
option5: false
}
},
{
code: 'test2',
name: 'TEST2',
url: '/test2',
elements: {
option1: false,
option2: false,
option3: true,
option4: false,
option5: false
}
},
].reduce((carry, current) => {
const existing = carry.find(item => item.code === current.code);
if (!existing) {
carry.push(current);
} else {
Object.entries(current.elements).forEach(([k, v]) => {
existing.elements[k] = existing.elements[k] || current.elements[k];
});
}
return carry;
}, []);
console.log(arr);