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.
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; }
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
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);
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.
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)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.