我有一个包含学生和家长地址的数组。
例如,
const users = [{
id: 1,
name: 'John',
email: 'johnson@mail.com',
age: 25,
parent_address: 'USA',
relationship:'mother'
},
{
id: 1,
name: 'John',
email: 'johnson@mail.com',
age: 25,
parent_address: 'Spain',
relationship:'father'
},
{
id: 2,
name: 'Mark',
email: 'mark@mail.com',
age: 28,
parent_address: 'France',
relationship:'father'
}
];
我正在尝试将其重新格式化为以下结果。
const list = [
{
id: 1,
name: 'John',
email: 'johnson@mail.com',
age: 25,
parent: [
{
parent_address: 'USA',
relationship:'mother'
},{
parent_address: 'Spain',
relationship:'father'
}
]
},
{
id: 2,
name: 'Mark',
email: 'mark@mail.com',
age: 28,
parent:[
{
parent_address: 'France',
relationship:'father'
}
]
}
];
到目前为止,我尝试了以下方式。我不确定这是否正确。
const duplicateInfo = [];
for (var i = 0; i < user[0].length; i++) {
var parent = [];
if (duplicateInfo.indexOf(user[0][i].id) != -1) {
// Do duplicate stuff
} else {
// Do other
}
duplicateInfo.push(user[0][i].id);
}
最佳答案
一种方法是使用 .reduce()
用一个对象作为累加器。对于每个 id,您可以存储一个关联的对象和一个可以附加到您的 .reduce()
callback 中的 parent 数组。每当您遇到具有相同 id 的新对象时。然后要从您的对象中获取对象数组,您可以调用 Object.values()
在上面
请参见下面的示例:
const users = [{ id: 1, name: 'John', email: 'johnson@mail.com', age: 25, parent_address: 'USA', relationship: 'mother' }, { id: 1, name: 'John', email: 'johnson@mail.com', age: 25, parent_address: 'Spain', relationship: 'father' }, { id: 2, name: 'Mark', email: 'mark@mail.com', age: 28, parent_address: 'France', relationship: 'father' } ];
const res = Object.values(users.reduce((acc, {parent_address, relationship, ...r}) => { // use destructuring assignment to pull out necessary values
acc[r.id] = acc[r.id] || {...r, parents: []}
acc[r.id].parents.push({parent_address, relationship}); // short-hand property names allows us to use the variable names as keys
return acc;
}, {}));
console.log(res);
由于您提到您是 JS 的新手,因此以更命令的方式可能更容易理解(有关详细信息,请参阅代码注释):
const users = [{ id: 1, name: 'John', email: 'johnson@mail.com', age: 25, parent_address: 'USA', relationship: 'mother' }, { id: 1, name: 'John', email: 'johnson@mail.com', age: 25, parent_address: 'Spain', relationship: 'father' }, { id: 2, name: 'Mark', email: 'mark@mail.com', age: 28, parent_address: 'France', relationship: 'father' } ];
const unique_map = {}; // create an object - store each id as a key, and an object with a parents array as its value
for(let i = 0; i < users.length; i++) { // loop your array object
const user = users[i]; // get the current object
const id = user.id; // get the current object/users's id
if(!(id in unique_map)) // check if current user's id is in the the object
unique_map[id] = { // add the id to the unique_map with an object as its associated value
id: id,
name: user.name,
email: user.email,
age: user.age,
parents: [] // add `parents` array to append to later
}
unique_map[id].parents.push({ // push the parent into the object's parents array
parent_address: user.parent_address,
relationship: user.relationship
});
}
const result = Object.values(unique_map); // get all values in the unique_map
console.log(result);
关于JavaScript 数组重构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59302132/