从现有对象创建新的对象数组,无需重复

时间:2018-03-01 15:26:08

标签: javascript json

我不认为我想要什么是特别的,我只是无法理解如何做到这一点。我有一个对象数组 -

{   
    "year": 2016,
    "some stuff": "bla0",
    "other stuff": 20
},
    "year": 2017,
    "some stuff": "bla1",
    "other stuff": 21
},
    "year": 2016,
    "some stuff": "bla2",
    "other stuff": 22
}

我想从上面创建以下对象数组。

{
    "2016": [
        {
            "year": 2016,
            "some stuff": "bla0",
            "other stuff": 20
        },
        {
            "year": 2016,
            "some stuff": "bla2",
            "other stuff": 22
        }
    ],
    "2017": [
        {
            "year": 2017,
            "some stuff": "bla1",
            "other stuff": 21
        }
    ]
}

我当前的WIP代码在https://codepen.io/sharperwebdev/pen/aqQQZy/?editors=1011相关的JS行从167开始。任何帮助我直接思考这个问题都将不胜感激。

5 个答案:

答案 0 :(得分:1)

使用函数reduce

var array = [{    "year": 2016,    "some stuff": "bla0",    "other stuff": 20  },  {    "year": 2017,    "some stuff": "bla1",    "other stuff": 21  },  {    "year": 2016,    "some stuff": "bla2",    "other stuff": 22  }]

var result = array.reduce((a, c) => {
  if (a[c.year]) a[c.year].push(c);
  else a[c.year] = [c];
  return a;
}, {});
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

答案 1 :(得分:1)

您想按年份对您的对象数组进行分组吗?你可以这样做:

const data = [{   
  "year": 2016,
  "some stuff": "bla0",
  "other stuff": 20
}, {
  "year": 2017,
  "some stuff": "bla1",
  "other stuff": 21
}, {
  "year": 2016,
  "some stuff": "bla2",
  "other stuff": 22
}];

function groupBy(data, key) {
  return data.reduce((result, item) => {
    result[item[key]] = result[item[key]] || [];
    result[item[key]].push(item);
    return result;
  }, {});
}

groupBy(data, 'year');

jsbin demo:http://jsbin.com/jifozivuve/edit?js,console

答案 2 :(得分:1)

var objArray=[{   
    "year": 2016,
    "some stuff": "bla0",
    "other stuff": 20
},{
    "year": 2017,
    "some stuff": "bla1",
    "other stuff": 21
},{
    "year": 2016,
    "some stuff": "bla2",
    "other stuff": 22
}];
var newObjArray={};

objArray.forEach(function(item){
var year=item.year;
  if(!newObjArray.hasOwnProperty(year)){
    newObjArray[year]=[item];
  }else{
    newObjArray[year].push(item);
  }
});
console.log(newObjArray);

答案 3 :(得分:1)

您可以使用forEach循环现有数组,并将year作为键添加到新对象。

var oldArr = [{
  "year": 2016,
  "some stuff": "bla0",
  "other stuff": 20
}, {
  "year": 2017,
  "some stuff": "bla1",
  "other stuff": 21
}, {
  "year": 2016,
  "some stuff": "bla2",
  "other stuff": 22
}]

var newObj = {};
oldArr.forEach(function(item) {
  if (newObj.hasOwnProperty(item.year)) {
    newObj[item.year].push(item)
  } else {
    newObj[item.year] = [];
    newObj[item.year].push(item)
  }
});
console.log(newObj)
I want to create the following array of objects from the above.

答案 4 :(得分:1)

要达到预期效果,请使用以下使用地图

的选项
var x = [{   
    "year": 2016,
    "some stuff": "bla0",
    "other stuff": 20
},{
    "year": 2017,
    "some stuff": "bla1",
    "other stuff": 21
},{
    "year": 2016,
    "some stuff": "bla2",
    "other stuff": 22
}]

var y =[]
x.map(function(e){
  var temp ={}
  temp[e.year] = e
  y.push(temp)
})

console.log(y)

https://codepen.io/nagasai/pen/yvZdQY