Reputation: 357
I don't think what I want is anything special, I just cant get my head round how to do it. I have an array of objects -
{
"year": 2016,
"some stuff": "bla0",
"other stuff": 20
},
"year": 2017,
"some stuff": "bla1",
"other stuff": 21
},
"year": 2016,
"some stuff": "bla2",
"other stuff": 22
}
I want to create the following array of objects from the above.
{
"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
}
]
}
My current WIP code is on https://codepen.io/sharperwebdev/pen/aqQQZy/?editors=1011 pertinent JS lines start at 167. Any help with getting my thinking straight on this would be appreciated.
Upvotes: 0
Views: 47
Reputation: 10975
To achieve expected result, use below option of using map
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
Upvotes: 1
Reputation: 50346
You can loop the existing array using forEach
and add year as akey to a new object.
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.
Upvotes: 1
Reputation: 5030
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);
Upvotes: 1
Reputation: 8991
You want to group your array of objects by the year? You can do something like:
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
Upvotes: 1
Reputation: 33736
Use the function 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; }
Upvotes: 1