• Empleos
  • Sobre nosotros
  • profesionales
    • Inicio
    • Empleos
    • Cursos y retos
  • empresas
    • Inicio
    • Publicar vacante
    • Nuestro proceso
    • Precios
    • Evaluaciones
    • Nómina
    • Blog
    • Comercial
    • Calculadora de salario

0

159
Vistas
Remove object array duplicates and store non duplicate values in array

I need to merge an array of deliveries that have notes, how would I remove the duplicate object but still keep the note string and store it in an array for the non duplicated object

Key begin the delivery number:

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

into

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

almost 3 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You can use Array.prototype.forEach() to loop over the notes array. If a note is encountered twice add their notes together.

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)
    
 

almost 3 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Nuestro proceso Comercial
Legal
Términos y condiciones Política de privacidad
© 2025 PeakU Inc. All Rights Reserved.

Andres GPT

Recomiéndame algunas ofertas
Necesito ayuda