Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

157
Vistas
better way push array of an object to array of an object based on the same id?

hi I want to push array of an object to array of an object so the the new property will push to the array of object based on the same _id this is the original data :

const data =[
             { 
              foo:foo1,
              data:{
                   _id:"a1",
                   man:2
                  }
            },
            { 
              foo:foo1,
              data:{
                   _id:"a1",
                   man:2
                }
             }
           ]

this is the data that I want to put on my original data

const d = [{
             _id:"a1",
             women:4,
           }]

And the desired output is:

      const data =[
             { 
              foo:foo1,
              data:{
                   _id:"a1",
                   man:2,
                   women:4
                  }
            },
            { 
              foo:foo1,
              data:{
                   _id:"a1",
                   man:2,
                   women:4
                }
             }
           ]

I think it can be done using for loop and check if the _id is the same and push it to the object, but is there any better way? or using lodash? any idea? thanks in advance

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

You can try this!

const d = [{
             _id:"a1",
             women:4,
           }
           ]
    
const data =[
             { 
              foo:"foo1",
              data:{
                   _id:"a1",
                   man:2
                  }
            },
            { 
              foo:"foo1",
              data:{
                   _id:"a1",
                   man:2
                }
             }
           ]    
var arr = data.map(function(obj, i){
    d.map(function(o,i){
    if(obj.data._id == o._id)
    {
        obj.data.women = o.women;
    }
    });
    return obj;
});

console.log(arr);

about 4 years ago · Juan Pablo Isaza Denunciar

0

With the data structures you have you can loop through both and simply update the entries with the same id. That said this is a brute force solution with O(n^2) time complexity. This solution also returns a new object rather than mutating the original.

// Existing entries
const entries = [
    {
        data: {
            id: 'a1',
            man: 2
        }
    },
    {
        data: {
            id: 'a1',
            man: 2
        }
    },
];

// Data with same id to update
const updates = [
    {
        id: 'a1',
        women: 4
    },
];

/**
 * Update properties for an existing entry given
 * a list of partially updated entries
 */
function updateEntries(entriesList, updatesList) {
    // Build up new array as to not mutate
    // the existing data structure
    const newEntries = [];
    for (const entry of entriesList) {
        for (const update of updatesList) {
            // Update when the ids match
            if (entry.data.id === update.id) {
                newEntries.push({
                    data: {
                        ...entry.data,
                        ...update
                    }
                });
            } else {
                newEntries.push(entry);
            }
        }
    }
    return newEntries;
}

const newEntries = updateEntries(entries, updates);
// newEntries = [
//     {
//         data: {
//             id: 'a1',
//             man: 2,
//             women: 4
//         }
//     },
//     {
//         data: {
//             id: 'a1',
//             man: 2,
//             women: 4
//         }
//     },
// ];

If you change your data structure for entries to be and object with each entry organized by id assuming they should actually be unique anyway, you can get O(n) based on updates.

// Entries by id
const entries = {
    a1: {
        man: 2
    },
    a2: {
        man: 2
    },
};
about 4 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 Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda