-1
[
  {
    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.

  • https://stackoverflow.com/questions/40774697/how-can-i-group-an-array-of-objects-by-key – epascarello Jan 24 '22 at 20:33
  • not exacly I don't want it to be repeated person values. [ { personId: 1, name: 'John', surName: 'Sur', lendingBooks:[ {bookId:1}, {bookId:2} .... ] } ] like this I'm trying to do it about 2 hour but I cant handle it :) – Yücel Bengü Jan 24 '22 at 20:55
  • It is the same basic idea. Instead of just having an array you have an object and you push into the array of the object. – epascarello Jan 24 '22 at 22:02

4 Answers4

2

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);
James
  • 20,957
  • 5
  • 26
  • 41
0

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}})));
MWO
  • 2,627
  • 2
  • 10
  • 25
0
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
      } 
    })
Christophvh
  • 12,586
  • 7
  • 48
  • 70
Cadet
  • 376
  • 1
  • 9
0

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));
Giancarl021
  • 511
  • 2
  • 12