group array of objects by id in javascript

kh

There are three such objects.

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

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 do not know what to do. Using object.assign doesn't seem to be the solution.

Suman Maji

The time complexity of this solution isO(n)

Well done:

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)

explain:

  1. allObjis the concatenation of all objects
  2. allObj.forEachlooping over all elements (child objects)
  3. Assignment using the ternary operator
  4. Assign if obj._id(key) already exists in (this is the concatenation of the existing value and the new value)preResult{...preResult[obj._id], ...obj}
  5. Otherwise assign new valueobj
  6. preResult is a pair of obj._id (key) and merged object (value)
  7. The end result is a unique valuepreResult

code segment:

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)

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324319616&siteId=291194637