最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Convert array of objects to an object of arrays - Stack Overflow

programmeradmin4浏览0评论

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.

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.

Share Improve this question edited May 23, 2017 at 12:22 CommunityBot 11 silver badge asked Jun 23, 2016 at 10:04 PeterBPeterB 2,3293 gold badges22 silver badges34 bronze badges 4
  • What solution do you have? – Dmitri Pavlutin Commented Jun 23, 2016 at 10:09
  • Your object has error. You can't use - in key without " or '. – Mohammad Commented Jun 23, 2016 at 10:10
  • @DmitriPavlutin Looping over every object in turn, creating a new one. Too messy & dirty to post here, it's an embarrassment – PeterB Commented Jun 23, 2016 at 10:11
  • @Tushar code added to the question – PeterB Commented Jun 23, 2016 at 10:20
Add a ment  | 

4 Answers 4

Reset to default 5

You can do it with the following snippet (no need for lodash etc):

const x = [{ '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 }, ];
let y = {};

x.forEach(obj => {
  Object.keys(obj).forEach(key => {
    y[key] = (y[key] || []).concat([obj[key]]);
  });
});

console.log(y)

Here is my one-liner. Uses map to map keys into array of objects with properties of the keys. Then maps the original array to only that property and sets it as the value of the property. One issue with this way is that it will only use the properties of the first object in the array. So if other objects have properties that aren't in the first object they will be ignored.

const output = Object.assign({}, ...Object.keys(input[0]).map(props => ({[props]: input.map(prop => prop[props])})))

Edit: the output was in the wrong format, fixed

You could iterate over the array and the over the keys of the object and build a new object with the keys as new keys.

var data = [{ '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 }, ],
    result = {};

data.forEach(function (o) {
    Object.keys(o).forEach(function (k) {
        result[k] = result[k] || [];
        result[k].push(o[k]);
    });
});

console.log(result);

Used Typescript -- obviously you can remove the types while working in JavaScript.

Assumption: The array of objects ing into the function will always have the same kind and number of object keys

mapperArrayOfJsonsToJsonOfArrays(inputArrayOfJsons: any): any {
    if (inputArrayOfJsons.length > 0) {
        let resultMap = {};
        let keys: any[] = Object.keys(inputArrayOfJsons[0]);
        keys.forEach((key: any) => {
            resultMap[key] = [];
        });

        inputArrayOfJsons.forEach((element: any) => {
            let values: any[] = Object.values(element);
            let index = 0;
            values.forEach((value: any) => {
                resultMap[keys[index]].push(value);
                index = index + 1;
            });
        });
        return resultMap;
    }
    return {};
}
发布评论

评论列表(0)

  1. 暂无评论