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

168
Vistas
Group array of objects by id in javascript

There are three objects like this.

var Object1 = [
    {
        "_id": "62397fd1c517e11e05935cc7",
        "nickname": "uknowTest2",
        "createdAt": "2022-03-22T07:50:41.769Z",
    },
    {
        "_id": "623824ccd1164bb8d9cc2521",
        "nickname": "qnvvzjrh",
        "createdAt": "2022-03-21T07:10:04.888Z",
        "animalCount": 1
    }
];

var Object2 = [
    {
        "_id": "62397fd1c517e11e05935cc7",
        "data": 36
    },
    {
        "_id": "623824ccd1164bb8d9cc2521",
        "data": 542717
    }
];

var Object3 = [
    {
        "_id": "62397fd1c517e11e05935cc7",
        "data2": 133111
    },
    {
        "_id": "623824ccd1164bb8d9cc2521",
        "data2": 540517
    }
];

And I want to group them like this:

var result = [
    {   
        "_id": "62397fd1c517e11e05935cc7",
        "data": 36,
        "data2": 133111,
        "nickname": "uknowTest2",
        "createdAt": "2022-03-22T07:50:41.769Z",
    },
    {
        "_id": "623824ccd1164bb8d9cc2521",
        "data": 542717,
        "data2": 540517,
        "nickname": "qnvvzjrh",
        "createdAt": "2022-03-21T07:10:04.888Z",
        "animalCount": 1
    }
];

Several similar types of data are generated. I'm not sure what to do. Using object.assign doesn't seem to be the solution.

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

0

This solution is taking time complexity of O(n)

Here you go:

const allObj = [...Object1, ...Object2, ...Object3];

const preResult = {};
allObj.forEach((obj)=> {
    preResult[obj._id] = preResult[obj._id] ? {...preResult[obj._id], ...obj} : obj;
})

const result = Object.values(preResult)
console.log(result)

Explaination:

  1. allObj is the concatenation of all objects
  2. allObj.forEach is looping through all elements (sub objects)
  3. Using ternary operator to assign the value
  4. If obj._id (key) already exist in preResult, then assign {...preResult[obj._id], ...obj} (which is concat of existed value and new value)
  5. Else assign new value obj
  6. preResult is pair of obj._id (key) and merged-object (values)
  7. final result is the only values of preResult

Code Snippet:

var Object1 = [
    {
        "_id": "62397fd1c517e11e05935cc7",
        "nickname": "uknowTest2",
        "createdAt": "2022-03-22T07:50:41.769Z",
    },
    {
        "_id": "623824ccd1164bb8d9cc2521",
        "nickname": "qnvvzjrh",
        "createdAt": "2022-03-21T07:10:04.888Z",
        "animalCount": 1
    }
];

var Object2 = [
    {
        "_id": "62397fd1c517e11e05935cc7",
        "data": 36
    },
    {
        "_id": "623824ccd1164bb8d9cc2521",
        "data": 542717
    }
];

var Object3 = [
    {
        "_id": "62397fd1c517e11e05935cc7",
        "data2": 133111
    },
    {
        "_id": "623824ccd1164bb8d9cc2521",
        "data2": 540517
    }
];

const allObj = [...Object1, ...Object2, ...Object3];

const preResult = {};
allObj.forEach((obj)=> {
    preResult[obj._id] = preResult[obj._id] ? {...preResult[obj._id], ...obj} : obj;
})

// console.log(preResult)

const result = Object.values(preResult)

console.log(result)

about 4 years ago · Juan Pablo Isaza Denunciar

0

var Object1 = [
    {
        "_id": "62397fd1c517e11e05935cc7",
        "nickname": "uknowTest2",
        "createdAt": "2022-03-22T07:50:41.769Z",
    },
    {
        "_id": "623824ccd1164bb8d9cc2521",
        "nickname": "qnvvzjrh",
        "createdAt": "2022-03-21T07:10:04.888Z",
        "animalCount": 1
    }
];

var Object2 = [
    {
        "_id": "62397fd1c517e11e05935cc7",
        "data": 36
    },
    {
        "_id": "623824ccd1164bb8d9cc2521",
        "data": 542717
    }
];

var Object3 = [
    {
        "_id": "62397fd1c517e11e05935cc7",
        "data2": 133111
    },
    {
        "_id": "623824ccd1164bb8d9cc2521",
        "data2": 540517
    }
];
let result = [...Object1];

let temp = [...result];
Object2.map(val=>{
  result.map((val2,index)=>{
    if(val2._id===val._id){
      val2 = {...val2,...val};
      temp[index] = val2
    }
  })
})
result = [...temp];
Object3.map(val=>{
  result.map((val2,index)=>{
    if(val2._id===val._id){
      val2 = {...val2,...val};
      temp[index] = val2
    }
  })
})
console.log(temp)

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