Skip to content
Advertisement

How to the modify the array of objects and return the latest updated field?

I have an object which I am getting from the DB, You can see the object which is coming from the DB This is what i’am getting from the DB.

[{
      id:1,
      cust_fname: "rock",
      cust_no:"C001",
      cust_need: "urgent",
      cust_place: "poland",
      date_created: "2021-04-16 18:50:40.658+00",
   },
   {
    id:1,
    cust_fname: "rock",
    cust_no:"C001",
    cust_need: "not-urgent",
    cust_place: "poland",
    date_created: "2021-04-16 19:50:40.658+00"
   },
   {
    id:2,
    cust_fname: "rmbo",
    cust_no:"C002",
    cust_need: "not-urgent",
    cust_place: "England",
    date_created: "2021-04-16 18:50:40.658+00"
   },
   {
    id:3,
    cust_fname: "ram",
    cust_no:"C004",
    cust_need: "urgent",
    cust_place: "USA",
    date_created: "2021-04-16 18:50:40.658+00"
   },
    {
    id:3,
    cust_fname: "ram",
    cust_no:"C004",
    cust_need: "not-urgent",
    cust_place: "USA",
    date_created: "2021-04-16 20:50:40.658+00"
   }
  ]

I want to modify the above object such that the it should only return latest inserted vlaue if the array of object has two same object with the same id along with all the other object.

just posting the object output which I want for more clarity.

removing the object with id:1 which has date_created: “2021-04-16 18:50:40.658+00”;

[{
   id:1,
   cust_fname: "rock",
   cust_no:"C001",
   cust_need: "not-urgent",
   cust_place: "poland",
   date_created: "2021-04-16 19:50:40.658+00"
  },
  {
   id:2,
   cust_fname: "rmbo",
   cust_no:"C002",
   cust_need: "not-urgent",
   cust_place: "England",
   date_created: "2021-04-16 18:50:40.658+00"
  },
  {
   id:3,
   cust_fname: "ram",
   cust_no:"C004",
   cust_need: "urgent",
   cust_place: "USA",
   date_created: "2021-04-16 20:50:40.658+00"
  }
 ]

Looking for solution how i can modify this obj or any other way also be aprriciated.

Advertisement

Answer

Here is a sort and a filter

You could alternatively use a Set.

const data = [{ id:1, cust_fname: "rock", cust_no:"C001", cust_need: "urgent", cust_place: "poland", date_created: "2021-04-16 18:50:40.658+00", }, { id:1, cust_fname: "rock", cust_no:"C001", cust_need: "not-urgent", cust_place: "poland", date_created: "2021-04-16 19:50:40.658+00" }, { id:2, cust_fname: "rmbo", cust_no:"C002", cust_need: "not-urgent", cust_place: "England", date_created: "2021-04-16 18:50:40.658+00" }, { id:3, cust_fname: "ram", cust_no:"C004", cust_need: "urgent", cust_place: "USA", date_created: "2021-04-16 18:50:40.658+00" }, { id:3, cust_fname: "ram", cust_no:"C004", cust_need: "not-urgent", cust_place: "USA", date_created: "2021-04-16 20:50:40.658+00" } ];
  
  let newData = data.slice(0); // copy the data
  newData = newData.sort((a,b) =>  {  
    if (a.id===b.id) { // if same ID sort date in descending order
      if (b.date_created > a.date_created) return 1; 
      if (a.date_created > b.date_created) return -1
    }  
    else return (a.id < b.id) ? -1 : 1; // else sort in ascending order
  }).filter(({id},i) => { // filter on unique IDs
    if (i>0) return id!==newData[i-1].id
    return true;
  }) 
  console.log(newData)
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement