【问题标题】:How to the modify the array of objects and return the latest updated field?如何修改对象数组并返回最新更新的字段?
【发布时间】:2021-07-11 20:17:09
【问题描述】:

我有一个从数据库获取的对象,您可以看到来自数据库的对象 这就是我从数据库中得到的。

[{
      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"
   }
  ]

我想修改上面的对象,这样它应该只返回最新插入的值,如果对象数组有两个具有相同 id 的相同对象以及所有其他对象。

只是发布我想要更清晰的对象输出。

移除 id:1 且 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"
  }
 ]

寻找解决方案,我可以如何修改这个 obj 或任何其他方式也可以。

【问题讨论】:

标签: javascript node.js reactjs postgresql express


【解决方案1】:

这是一个排序和一个过滤器

您也可以使用 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)

【讨论】:

  • 谢谢,但是当我尝试使用修改后的对象的相同逻辑时,没有返回相同的结果。我已经更新了 obj 和想要的结果。
  • 谢谢,如果可能的话,你能补充解释吗?
  • @Rambocheck 完成
猜你喜欢
  • 2019-09-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-14
  • 1970-01-01
  • 2018-06-09
  • 1970-01-01
  • 2022-11-01
相关资源
最近更新 更多