Quantcast
Channel: Active questions tagged javascript - Stack Overflow
Viewing all articles
Browse latest Browse all 140762

How to deep merge all objects in array of objects using javascript

$
0
0

I have an array of objects in which I would like to merge all objects into a single object.

For example:

arrObj = [{
    "id1": {
      "subId1": {
        "data1": 10
      }
    },
    "id2": {
      "subId1": {
        "data1": 20
      }
    }
  },
  {
    "id1": {
      "subId1": {
        "data2": 20
      }
    },
    "id2": {
      "subId2": {
        "data2": 30
      }
    }
  }
];

Expected result:

resObj = {
  "id1": {
    "subId1": {
      "data1": 10,
      "data2": 20
    }
  },
  "id2": {
    "subId1": {
      "data1": 20,
    },
    "subId2": {
      "data2": 30
    }
  }
}

I tried

Object.assign({},...arrObj)

But it is not satisfying the expected result. How to merge all objects in an array of objects as the expected result


Viewing all articles
Browse latest Browse all 140762

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>