如何通过 javascript 中的键合并对象数组
How to merge array of objects by its key in javascript
我有一组像这样的对象:
[
{
user: {
key1: ['1', '2', '3'],
},
},
{
user: {
key2: ['3', '4', '5'],
},
},
{
user2: {
key1: ['1', '2', '3'],
},
},
{
user2: {
key2: ['3', '4', '5'],
},
},
....
];
我需要通过它的键来过滤那些并期待像这样的输出
[
{
user: {
key1: ['1', '2', '3'],
key2: ['3', '4', '5'],
},
},
{
user2: {
key1: ['1', '2', '3'],
key2: ['3', '4', '5'],
....
}
}
]
这里的user,user2可以是任意key(userName)也可以是key1,key 2等等...可以是任意key(userActivity),字符串数组。
这是对象的类型:
[key: string]: {
[key: string]: string[];
};
}[];
过滤此内容的最佳方法如有任何帮助,我们将不胜感激
一个想法 array.reduce
var data = [{
user: {
key1: ['1', '2', '3'],
},
},
{
user: {
key2: ['3', '4', '5'],
},
},
{
user2: {
key1: ['1', '2', '3'],
},
},
{
user2: {
key2: ['3', '4', '5'],
},
}
];
let result = data.reduce((acc, curr) => {
Object.keys(curr).forEach(key => {
let found = acc.find(elem => elem[key]);
if (found) {
found[key] = {...found[key], ...curr[key]}
} else {
acc.push(curr);
}
});
return acc;
}, []);
console.log(result);
您可以使用 reduce()
方法:
- 遍历
data
数组中的每一项
- 检查项目中的
key
是否已存在于最终结果中
- 如果存在,则连接当前值和新值
- 如果不存在,则初始化新值
const data = [
{ user: { key1: ['1', '2', '3'] } },
{ user: { key2: ['3', '4', '5'] } },
{ user2: { key1: ['1', '2', '3'] } },
{ user2: { key2: ['3', '4', '5'] } }
];
const result = data.reduce((accumulator, currentValue)=>{
const currentKey = Object.keys(currentValue)[0];
if(Object.keys(accumulator).includes(currentKey)) {
accumulator[currentKey] = {...accumulator[currentKey], ...currentValue[currentKey]};
} else {
accumulator[currentKey] = currentValue[currentKey];
}
return accumulator;
},{})
console.log(result);
也许是这样的
const initial = [
{
user: {
key1: ['1', '2', '3'],
},
},
{
user: {
key2: ['3', '4', '5'],
},
},
{
user2: {
key1: ['1', '2', '3'],
},
},
{
user2: {
key2: ['3', '4', '5'],
},
},
];
let result = initial.reduce((acc, curr)=>{
if(Object.keys(curr)[0]) {
const key =Object.keys(curr)[0]
const values = curr[key]
acc = {
...acc,
[key]: {
...acc[key],
...values
}
}
return acc
}
},[])
我有一组像这样的对象:
[
{
user: {
key1: ['1', '2', '3'],
},
},
{
user: {
key2: ['3', '4', '5'],
},
},
{
user2: {
key1: ['1', '2', '3'],
},
},
{
user2: {
key2: ['3', '4', '5'],
},
},
....
];
我需要通过它的键来过滤那些并期待像这样的输出
[
{
user: {
key1: ['1', '2', '3'],
key2: ['3', '4', '5'],
},
},
{
user2: {
key1: ['1', '2', '3'],
key2: ['3', '4', '5'],
....
}
}
]
这里的user,user2可以是任意key(userName)也可以是key1,key 2等等...可以是任意key(userActivity),字符串数组。
这是对象的类型:
[key: string]: {
[key: string]: string[];
};
}[];
过滤此内容的最佳方法如有任何帮助,我们将不胜感激
一个想法 array.reduce
var data = [{
user: {
key1: ['1', '2', '3'],
},
},
{
user: {
key2: ['3', '4', '5'],
},
},
{
user2: {
key1: ['1', '2', '3'],
},
},
{
user2: {
key2: ['3', '4', '5'],
},
}
];
let result = data.reduce((acc, curr) => {
Object.keys(curr).forEach(key => {
let found = acc.find(elem => elem[key]);
if (found) {
found[key] = {...found[key], ...curr[key]}
} else {
acc.push(curr);
}
});
return acc;
}, []);
console.log(result);
您可以使用 reduce()
方法:
- 遍历
data
数组中的每一项 - 检查项目中的
key
是否已存在于最终结果中 - 如果存在,则连接当前值和新值
- 如果不存在,则初始化新值
const data = [
{ user: { key1: ['1', '2', '3'] } },
{ user: { key2: ['3', '4', '5'] } },
{ user2: { key1: ['1', '2', '3'] } },
{ user2: { key2: ['3', '4', '5'] } }
];
const result = data.reduce((accumulator, currentValue)=>{
const currentKey = Object.keys(currentValue)[0];
if(Object.keys(accumulator).includes(currentKey)) {
accumulator[currentKey] = {...accumulator[currentKey], ...currentValue[currentKey]};
} else {
accumulator[currentKey] = currentValue[currentKey];
}
return accumulator;
},{})
console.log(result);
也许是这样的
const initial = [
{
user: {
key1: ['1', '2', '3'],
},
},
{
user: {
key2: ['3', '4', '5'],
},
},
{
user2: {
key1: ['1', '2', '3'],
},
},
{
user2: {
key2: ['3', '4', '5'],
},
},
];
let result = initial.reduce((acc, curr)=>{
if(Object.keys(curr)[0]) {
const key =Object.keys(curr)[0]
const values = curr[key]
acc = {
...acc,
[key]: {
...acc[key],
...values
}
}
return acc
}
},[])