如果所述对象中的其他值相同,则合并对象中的值

时间:2018-06-21 02:35:28

标签: javascript angular

例如,我有一个像这样的数组。

[{
    "Country": "USA",   "Nickname": "John"
}, {
    "Country": "Japan", "Nickname": "Grace"
}, {
    "Country": "Japan", "Nickname": "Mark"
}, {
    "Country": "Japan", "Nickname": "Paul"
}, {
    "Country": "China", "Nickname": "Sansa"
}, {
    "Country": "USA",   "Nickname": "Clint"
}, {
    "Country": "China", "Nickname": "James"
}, {
    "Country": "Japan", "Nickname": "Mary"
}]

我希望我的数组看起来像这样。

[{
    "Country": "USA",   "Nickname": "John , Clint"
}, {
    "Country": "Japan", "Nickname": "Grace , Mark , Paul , Mary"
}, {
    "Country": "China", "Nickname": "Sansa, James"
}]

我想按他们的国家/地区合并昵称。我该怎么办?

4 个答案:

答案 0 :(得分:0)

这是一个由对象和数组完成的简单示例。

下次,您完全可以自己尝试一下,老兄。

var arr = [{
    "Country": "USA",   "Nickname": "John"
}, {
    "Country": "Japan", "Nickname": "Grace"
}, {
    "Country": "Japan", "Nickname": "Mark"
}, {
    "Country": "Japan", "Nickname": "Paul"
}, {
    "Country": "China", "Nickname": "Sansa"
}, {
    "Country": "USA",   "Nickname": "Clint"
}, {
    "Country": "China", "Nickname": "James"
}, {
    "Country": "Japan", "Nickname": "Mary"
}]

var obj = {};
var new_arr = [];

for(var i=0;i<arr.length;i++){
  var d = arr[i];
  if(!obj[d['Country']])
    obj[d['Country']] = [];
  obj[d['Country']].push(d['Nickname']);  
}

for(var key in obj){
  new_arr.push({
    "Country": key,
    "Nickname": obj[key].join(' , ')
  })
}
console.log(new_arr);

答案 1 :(得分:0)

let arr = [{"Country": "USA", "Nickname": "John"}, {"Country": "Japan", "Nickname": "Grace"}, {"Country": "Japan", "Nickname": "Mark"}, {"Country": "Japan", "Nickname": "Paul"}, {"Country": "China", "Nickname": "Sansa"}, {"Country": "USA",   "Nickname": "Clint"}, {"Country": "China", "Nickname": "James"}, {"Country": "Japan", "Nickname": "Mary"}];

let answer = [];
arr.forEach(x=>{
  if(!answer.some(y => y.Country === x.Country)){
    answer.push(x);
  }else{
    let country = answer.find(y => y.Country === x.Country);
    country.Nickname = country.Nickname.concat(`, ${x.Nickname}`);
  }
});

console.log(answer)

使用some检查另一个数组中是否存在,如果不存在,则将该项目推到新数组中(如果存在),抓住它并附加昵称。

答案 2 :(得分:0)

我不久前也遇到了同样的问题,这是怎么做的,您可以通过简单地使用以下方法来实现:

let result = [];

arr.forEach(function(obj) {
    let id = obj.Country;

    if(!this[id]) {
        result.push(this[id] = obj);
    } else {
        this[id].Nickname += `, ${obj.Nickname}`;
    }
}, Object.create(null));

console.log(result);

结果将得到您想要的结果...并且我已经对其进行了测试...

在发布问题之前,您应该自己尝试...每个人都会给您同样的建议...我想您还是新手,但是下次您应该先尝试然后问您在哪里卡住... < / p>

答案 3 :(得分:0)

您可以使用reduce将数组分组为一个对象。使用Object.entries将对象转换为数组。使用map格式化数组。

var arr = [{"Country":"USA","Nickname":"John"},{"Country":"Japan","Nickname":"Grace"},{"Country":"Japan","Nickname":"Mark"},{"Country":"Japan","Nickname":"Paul"},{"Country":"China","Nickname":"Sansa"},{"Country":"USA","Nickname":"Clint"},{"Country":"China","Nickname":"James"},{"Country":"Japan","Nickname":"Mary"}];

var result = Object.entries(arr.reduce((c, v) => {
  c[v.Country] = c[v.Country] || [];
  c[v.Country].push(v.Nickname);
  return c;
}, {})).map(([k, v]) => ({"Country": k,"Nickname": v.join(' , ')}));

console.log(result);