【问题标题】: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
        }
    ]

【问题讨论】:

标签: javascript arrays duplicates


【解决方案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)
    
 

【讨论】:

    猜你喜欢
    • 2012-11-24
    • 2017-04-10
    • 2022-01-24
    • 2021-01-23
    • 2021-08-26
    • 1970-01-01
    • 1970-01-01
    • 2019-02-28
    相关资源
    最近更新 更多