将具有数组值的对象转换为另一个对象数组
Convert Object which has value an Array to another Array of Object
我有这样的对象:
{
"John":[
{
"address":"xxx1",
"city":"yyy1"
},
{
"address":"xxx2",
"city":"yyy2"
}
],
"Doe":[
{
"address":"aaaa1",
"city":"aaa1"
}
],
"Smith":[
{
"address":"bbb1",
"city":"bbb1"
}
],
}
我试图实现的是减少这个对象,使其看起来像这样:
[
{
"name":"John",
"address":"xxx1",
"city":"yyy1"
},
{
"name":"John",
"address":"xxx2",
"city":"yyy2"
},
{
"name":"Doe",
"address":"aaaa1",
"city":"aaaa1"
},
{
"name":"Smith",
"address":"bbb1",
"city":"bbb1"
}
]
但我确信同样的事情可以通过使用 ES6 array.reduce
以某种方式完成。你能帮助我吗?我看了 JS (ES6): Reduce array based on object attribute 但我想不通。
const modifiedData = Object.entries(data).reduce(function (acc, [key,value]) {
const personName = key;
return [
...acc,
{
Agent: personName ,
adress: value.adress
},
];
}, []);
如果你想使用 Array.prototype.reduce
来做,你可以这样做:
const input = {
"John": [{
"address": "xxx1",
"city": "yyy1"
},
{
"address": "xxx2",
"city": "yyy2"
}
],
"Doe": [{
"address": "aaaa1",
"city": "aaa1"
}
],
"Smith": [{
"address": "bbb1",
"city": "bbb1"
}],
}
// 1. Using Object.keys()
const output1 = Object.keys(input).reduce((acc, person) => {
input[person].forEach(item => {
acc.push({ name: person, ...item })
})
return acc;
}, []);
console.log('output1:', output1)
// 2. Using Object.entries()
const output2 = Object.entries(input).reduce((acc, [key, value]) => {
value.forEach(item => {
acc.push({ name: key, ...item })
});
return acc;
}, [])
console.log('output2:', output2);
您可以使用 reduce 来实现。
const obj = {
John: [
{
address: "xxx1",
city: "yyy1",
},
{
address: "xxx2",
city: "yyy2",
},
],
Doe: [
{
address: "aaaa1",
city: "aaa1",
},
],
Smith: [
{
address: "bbb1",
city: "bbb1",
},
],
};
const result = Object.entries(obj).reduce((acc, [key, arr]) => {
const collection = arr.map((a) => ({ name: key, ...a }));
acc = [...acc, ...collection];
return acc;
}, []);
console.log( result );
像这样的简单方法。
const data = {"John":[{"address":"xxx1","city":"yyy1"},{"address":"xxx2","city":"yyy2"}],"Doe":[{"address":"aaaa1","city":"aaa1"}],"Smith":[{"address":"bbb1","city":"bbb1"}],};;
const result = Object.entries(data).flatMap(([key, values]) =>
values.map(o => ({name: key, ...o})));
console.log(result);
我有这样的对象:
{
"John":[
{
"address":"xxx1",
"city":"yyy1"
},
{
"address":"xxx2",
"city":"yyy2"
}
],
"Doe":[
{
"address":"aaaa1",
"city":"aaa1"
}
],
"Smith":[
{
"address":"bbb1",
"city":"bbb1"
}
],
}
我试图实现的是减少这个对象,使其看起来像这样:
[
{
"name":"John",
"address":"xxx1",
"city":"yyy1"
},
{
"name":"John",
"address":"xxx2",
"city":"yyy2"
},
{
"name":"Doe",
"address":"aaaa1",
"city":"aaaa1"
},
{
"name":"Smith",
"address":"bbb1",
"city":"bbb1"
}
]
但我确信同样的事情可以通过使用 ES6 array.reduce
以某种方式完成。你能帮助我吗?我看了 JS (ES6): Reduce array based on object attribute 但我想不通。
const modifiedData = Object.entries(data).reduce(function (acc, [key,value]) {
const personName = key;
return [
...acc,
{
Agent: personName ,
adress: value.adress
},
];
}, []);
如果你想使用 Array.prototype.reduce
来做,你可以这样做:
const input = {
"John": [{
"address": "xxx1",
"city": "yyy1"
},
{
"address": "xxx2",
"city": "yyy2"
}
],
"Doe": [{
"address": "aaaa1",
"city": "aaa1"
}
],
"Smith": [{
"address": "bbb1",
"city": "bbb1"
}],
}
// 1. Using Object.keys()
const output1 = Object.keys(input).reduce((acc, person) => {
input[person].forEach(item => {
acc.push({ name: person, ...item })
})
return acc;
}, []);
console.log('output1:', output1)
// 2. Using Object.entries()
const output2 = Object.entries(input).reduce((acc, [key, value]) => {
value.forEach(item => {
acc.push({ name: key, ...item })
});
return acc;
}, [])
console.log('output2:', output2);
您可以使用 reduce 来实现。
const obj = {
John: [
{
address: "xxx1",
city: "yyy1",
},
{
address: "xxx2",
city: "yyy2",
},
],
Doe: [
{
address: "aaaa1",
city: "aaa1",
},
],
Smith: [
{
address: "bbb1",
city: "bbb1",
},
],
};
const result = Object.entries(obj).reduce((acc, [key, arr]) => {
const collection = arr.map((a) => ({ name: key, ...a }));
acc = [...acc, ...collection];
return acc;
}, []);
console.log( result );
像这样的简单方法。
const data = {"John":[{"address":"xxx1","city":"yyy1"},{"address":"xxx2","city":"yyy2"}],"Doe":[{"address":"aaaa1","city":"aaa1"}],"Smith":[{"address":"bbb1","city":"bbb1"}],};;
const result = Object.entries(data).flatMap(([key, values]) =>
values.map(o => ({name: key, ...o})));
console.log(result);