如何在JavaScript对象中合并重复键?

时间:2017-08-21 05:08:04

标签: javascript

我有一个JavaScript对象如下:

{
    "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”中的重复键,并在“a”键中附加值

{
    "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"
            },
            {
                "name": "javis",
                "ip": "192.168.0.4"
            },
            {
                "name": "jump",
                "ip": "192.168.0.5"
            },
            {
                "name": "jupiter",
                "ip": "192.168.0.6"
            }
        ]

    }]
}

我知道如何从其他主题合并来自两个不同对象的重复键,但我不知道如何在同一个对象中合并和查找重复键。

2 个答案:

答案 0 :(得分:1)

首先,使用reduce收集重复项并将其保存在值为$origin的临时对象中。然后迭代键并重建对象。

另一种方法是使用一些过滤来完成reduce方法中的大部分工作,但我认为我当前的解决方案更快。



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)




答案 1 :(得分:0)

您可以从索引1开始循环遍历数组,并继续按索引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 0
for (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 objects
    let dumObj = {};
    dumObj.name = item.name;
    dumObj.ip = item.ip;
    
    x.zone[0].a.push(dumObj);
  })
}

console.log(x)
&#13;
&#13;
&#13;