I have the following structure
var nights = [
{ "2016-06-25": 32, "2016-06-26": 151, "2016-06-27": null },
{ "2016-06-24": null, "2016-06-25": null, "2016-06-26": null },
{ "2016-06-26": 11, "2016-06-27": 31, "2016-06-28": 31 },
];
And I want to transform it to:
{
"2016-06-24": [null],
"2016-06-25": [32, null],
"2016-06-26": [151, null, 11],
"2016-06-27": [null, 31],
"2016-06-28": [31]
}
What's the shortest way to solve this? I have no problems with using Underscore, Lodash or jQuery.
My current code is:
var out = {};
for (var i = 0; i < nights.length; i++) {
for (var key in nights[i]) {
if (out[key] === undefined) {
out[key] = [];
}
out[key].push(nights[i][key]);
}
}
It's similar to Convert array of objects to object of arrays using lodash but that has all keys present in each object.
-inkeywithout"or'.