Skip to content Skip to sidebar Skip to footer

How Can I Merge Duplicate Keys In A Javascript Object?

I have a JavaScript object as follows : { 'zone': [{ '$origin': 'domainname.com.', 'a': [{ 'name': 'ironman', 'ip': '192.168.0.1

Solution 1:

First of all, use reduce to collect the duplicates and save them in a temporary object with values of $origin as its keys. Then iterate the keys and reconstruct the the object.

Another way would be doing most of the work in the reduce method with some filtering but I think my current solution is faster.

const data = {
    "zone": [{
        "$origin": "domainname.com.",
        "a": [{
                "name": "ironman",
                "ip": "192.168.0.1"
            }, {
                "name": "thor",
                "ip": "192.168.0.2"
            },
            {
                "name": "odin",
                "ip": "192.168.0.3"
            }
        ]

    }, {
        "$origin": "domainname.com.",
        "a": [{
                "name": "javis",
                "ip": "192.168.0.4"
            },
            {
                "name": "jump",
                "ip": "192.168.0.5"
            },
            {
                "name": "jupiter",
                "ip": "192.168.0.6"
            }
        ]
    },
    {
        "$origin": "eomainname.com.",
        "a": [{
                "name": "javis",
                "ip": "192.168.0.4"
            },
            {
                "name": "jump",
                "ip": "192.168.0.5"
            },
            {
                "name": "jupiter",
                "ip": "192.168.0.6"
            }
        ]
    }]
}

const result = { zone: [] }
const tmp = data.zone.reduce((acc, curr) => {
  if (acc.hasOwnProperty(curr.$origin)) {
    acc[curr.$origin] = acc[curr.$origin].concat(curr.a)
  } else {
    acc[curr.$origin] = curr.a
  }
  return acc;
}, {})

result.zone = Object.keys(tmp).map((key) => {
  return {
    $origin: key,
    a: tmp[key]
  }
})

console.log(result)

Solution 2:

You can loop through the array starting from index 1 and keep on pushing the values in the object at index 0

let x = {
  "zone": [{
    "$origin": "domainname.com.",
    "a": [{
        "name": "ironman",
        "ip": "192.168.0.1"
      }, {
        "name": "thor",
        "ip": "192.168.0.2"
      },
      {
        "name": "odin",
        "ip": "192.168.0.3"
      }
    ]

  }, {
    "$origin": "domainname.com.",
    "a": [{
        "name": "javis",
        "ip": "192.168.0.4"
      },
      {
        "name": "jump",
        "ip": "192.168.0.5"
      },
      {
        "name": "jupiter",
        "ip": "192.168.0.6"
      }
    ]
  }]
}
// starting to loop from index 1, new elements will be pushed
  to the object at index 0for (var i = 1; i < x.zone.length; i++) {
  // looping through the array of object at index 1 ...
  x.zone[i].a.forEach(function(item) {
    // dummy object which will have values from other objectslet dumObj = {};
    dumObj.name = item.name;
    dumObj.ip = item.ip;
    
    x.zone[0].a.push(dumObj);
  })
}

console.log(x)

Post a Comment for "How Can I Merge Duplicate Keys In A Javascript Object?"