删除对象数组重复项并将非重复值存储在数组中

Posted

技术标签:

【中文标题】删除对象数组重复项并将非重复值存储在数组中【英文标题】:Remove object array duplicates and store non duplicate values in array 【发布时间】:2021-11-25 01:20:11 【问题描述】:

我需要合并一个包含备注的交货数组,如何删除重复的对象但仍保留备注字符串并将其存储在非重复对象的数组中

关键开始交货号:

"data": [
        
            "deliveryNumber": "0000001",
            "deliveryDate": "2021-10-01T00:00:00.000Z",
            "dateBeginProcess": null,
            "dateFinishedProcess": null,
            "status": "",
            "notes": "Note 1"
        ,
        
            "deliveryNumber": "0000001",
            "deliveryDate": "2021-10-01T00:00:00.000Z",
            "dateBeginProcess": null,
            "dateFinishedProcess": null,
            "status": "Ready",
            "notes": "Note 2"
        ,
        
            "deliveryNumber": "0000002",
            "deliveryDate": "2021-10-01T14:21:00.000Z",
            "dateBeginProcess": null,
            "dateFinishedProcess": null,
            "status": "Ready",
            "notes": null
        
    ]

进入

"data": [
        
            "deliveryNumber": "0000001",
            "deliveryDate": "2021-10-01T00:00:00.000Z",
            "dateBeginProcess": null,
            "dateFinishedProcess": null,
            "status": "Ready",
            "notes": ["Note 1", "Note 2"]
        ,
        
            "deliveryNumber": "0000002",
            "deliveryDate": "2021-10-01T14:21:00.000Z",
            "dateBeginProcess": null,
            "dateFinishedProcess": null,
            "status": "Ready",
            "notes": null
        
    ]

【问题讨论】:

欢迎来到 Stack Overflow,请附上您的代码尝试。 这能回答你的问题吗? How to group an array of objects by key? 或者这个Group array items using object 【参考方案1】:

您可以使用 Array.prototype.forEach() 循环遍历 notes 数组。如果遇到两次注释,请将它们的注释加在一起。

const notes = [
        
            "deliveryNumber": "0000001",
            "deliveryDate": "2021-10-01T00:00:00.000Z",
            "dateBeginProcess": null,
            "dateFinishedProcess": null,
            "status": "",
            "notes": "Note 1"
        ,
        
            "deliveryNumber": "0000001",
            "deliveryDate": "2021-10-01T00:00:00.000Z",
            "dateBeginProcess": null,
            "dateFinishedProcess": null,
            "status": "Ready",
            "notes": "Note 2"
        ,
        
            "deliveryNumber": "0000002",
            "deliveryDate": "2021-10-01T14:21:00.000Z",
            "dateBeginProcess": null,
            "dateFinishedProcess": null,
            "status": "Ready",
            "notes": null
        
    ]
    
    let filteredArray = []
    
    notes.forEach(note => 
      let noteFound = filteredArray.find(el => el.deliveryNumber === note.deliveryNumber)
      if(noteFound)
         // not first encounter
         // add notes together
         noteFound.notes.push(note.notes)
      else
         // first encounter
         // make notes an array
         note.notes = [note.notes||'']
         filteredArray.push(note)
      
    )
    
    console.log(filteredArray)
    
 

【讨论】:

以上是关于删除对象数组重复项并将非重复值存储在数组中的主要内容,如果未能解决你的问题,请参考以下文章

获取嵌套数组/对象的数组中的所有唯一值(删除重复项)

使用值将数组中对象的属性合并在一起并删除重复项

从 GeoFire 对象数组中删除重复项 [重复]

如何将 json 对象键转换为不同的数组来删除重复项

从数组中删除重复的对象

删除排序数组中的重复项并将它们推送到新数组中