[
{
personId: 1,
name: 'John',
surName: 'Sur',
bookId: 1,
bookName: 'Lord of the ring',
updated_at: 2022-01-24T19:20:53.742Z,
deliver__date: null
},
{
personId: 1,
name: 'John',
surName: 'Sur',
bookId: 2,
bookName: 'Fableheaven',
updated_at: 2022-01-24T19:21:57.813Z,
deliver__date: null
},
{
personId: 1,
name: 'John',
surName: 'Sur',
bookId: 3,
bookName: 'Some Book',
updated_at: 2022-01-24T19:21:57.813Z,
deliver__date: null
}
]
I wanna do this json data like this [{ personId:1 name:"John", surName:"Sur", lend:{..rest} }]
but I can't how can do this thanks for help for now.
CodePudding user response:
result = arr.map(el => {
return {
personId: el.personId,
name: el.name,
surName: el.surName,
lend: {
bookId: el.bookId,
bookNme: el.bookName,
updated_at: el.updated_at,
deliver__date: el.deliver__date} }})
CodePudding user response:
Im not sure if I got your requirements correctly, but do you mean this?
const obj = [ { personId: 1, name: 'John', surName: 'Sur', bookId: 1, bookName: 'Lord of the ring', updated_at: '2022-01-24T19:20:53.742Z', deliver__date: null }, { personId: 1, name: 'John', surName: 'Sur', bookId: 2, bookName: 'Fableheaven', updated_at: '2022-01-24T19:21:57.813Z', deliver__date: null }, { personId: 1, name: 'John', surName: 'Sur', bookId: 3, bookName: 'Some Book', updated_at: '2022-01-24T19:21:57.813Z', deliver__date: null } ]
console.log(obj.map((el)=>({personId: el.personId, name: el.name, surName: el.surName,
lend: {bookId: el.bookId, bookName: el.bookName, updated_at: el.updated_at, deliver__date: el.deliver__date}})));
CodePudding user response:
I don't think it will be a single-line and optimized way to aggregate the data (if that is your intention, otherwise the @MWO answer already have what you need), so, I would do something like this:
const data = [{
personId: 1,
name: 'John',
surName: 'Sur',
bookId: 1,
bookName: 'Lord of the ring',
updated_at: '2022 - 01 - 24 T19: 20: 53.742 Z',
deliver__date: null
},
{
personId: 1,
name: 'John',
surName: 'Sur',
bookId: 2,
bookName: 'Fableheaven',
updated_at: '2022 - 01 - 24 T19: 21: 57.813 Z',
deliver__date: null
},
{
personId: 1,
name: 'John',
surName: 'Sur',
bookId: 3,
bookName: 'Some Book',
updated_at: '2022 - 01 - 24 T19: 21: 57.813 Z',
deliver__date: null
}
];
function aggregateByPerson(input) {
const map = {};
input.forEach(entry => {
const {
personId,
name,
surName,
...lend
} = entry;
if (map[entry.personId]) {
map[entry.personId].lend.push(lend);
} else {
map[entry.personId] = {
personId,
name,
surName,
lend: [lend]
};
}
});
return Object.values(map);
}
console.log(aggregateByPerson(data));
CodePudding user response:
It seems like you want to use "Rest Parameters" by the language of your question. More Info
const input = [
{
personId: 1,
name: 'John',
surName: 'Sur',
bookId: 1,
bookName: 'Lord of the ring',
updated_at: '2022-01-24T19:20:53.742Z',
deliver__date: null
},
{
personId: 1,
name: 'John',
surName: 'Sur',
bookId: 2,
bookName: 'Fableheaven',
updated_at: '2022-01-24T19:21:57.813Z',
deliver__date: null
},
{
personId: 1,
name: 'John',
surName: 'Sur',
bookId: 3,
bookName: 'Some Book',
updated_at: '2022-01-24T19:21:57.813Z',
deliver__date: null
}
];
const output = input.map(({personId, name, surName, ...rest}) => ({
personId,
name,
surName,
rest
}));
console.log(output);
