Roy Arturo
Roy Arturo

Reputation: 35

create array of values with same key from an array of objects

I have an array of objects that looks like this:

[
     {
      key1: val_1.a
      key2: val_2.a
      key3: val_3.a
     },{
      key1: val_1.b
      key2: val_2.b
      key3: val_3.b
     },{
      key1: val_1.c
      key2: val_2.c
      key3: val_3.c
     }
]

I want to use that object to create another object that looks like this:

{ 
    key1: [val_1.a, val_1.b, val_1.c]
    key2: [val_2.a, val_2.b, val_2.c]
    key3: [[val_3.a, val_3.b, val_3.c]
}

Since the keys are the same in each object I just want to save in an array all the values that correspond to each key.

Please, If there is anyone who have had to do this before and could share the code it would be great. Thanks in advance.

Upvotes: 3

Views: 6408

Answers (4)

Rose
Rose

Reputation: 71

let R = require('ramda');
let arr =[
    {
     'key1': 'a',
     'key2': 'b',
     'key3': 'c',
    },{
     'key1': 'a1',
     'key2': 'b1',
     'key3': 'c1',
    },{
     'key1': 'a2',
     'key2': 'b2',
     'key3': 'c2',
    }
];

let key1 = R.pluck('key1', arr);
let key2 = R.pluck('key2', arr);
let key3 = R.pluck('key3', arr);
let obj = {
     key1,
     key2,
     key3
};

Obj = { key1: [ 'a', 'a1', 'a2' ], key2: [ 'b', 'b1', 'b2' ], key3: [ 'c', 'c1', 'c2' ] }

Upvotes: 0

Madara's Ghost
Madara's Ghost

Reputation: 175098

var arr = [
 {
  key1: 'val_1.a',
  key2: 'val_2.a',
  key3: 'val_3.a'
 },{
  key1: 'val_1.b',
  key2: 'val_2.b',
  key3: 'val_3.b'
 },{
  key1: 'val_1.c',
  key2: 'val_2.c',
  key3: 'val_3.c'
 }
]

var result = arr.reduce(function(obj, current) { //Reduce the array to an object
    Object.keys(current).forEach(function(key) { //Each key
        obj[key] = obj[key] || []; //Has to be an array if not exists
        obj[key] = Array.isArray(obj[key]) ? obj[key] : [obj[key]]; //Has to be an array if not an array
        obj[key].push(current[key]); //Add current item to array of matching key
    });
    return obj; //Continue to the next object in the array
});

console.log(result);

The above functions: Array#reduce, Array#forEach, Array.isArray, Object.keys are all part of the ECMAScript 5 specifications, as a result, they are not available in IE8 and below.

Upvotes: 0

jfriend00
jfriend00

Reputation: 708146

You can do it like this in a way that will work in all browsers and will even work if all objects don't have the exact same set of keys (e.g. it will just accumulate data for whatever keys exist):

function combineKeyData(data) {
    var output = {}, item;
    // iterate the outer array to look at each item in that array
    for (var i = 0; i < data.length; i++) {
        item = data[i];
        // iterate each key on the object
        for (var prop in item) {
            if (item.hasOwnProperty(prop)) {
                // if this keys doesn't exist in the output object, add it
                if (!(prop in output)) {
                    output[prop] = [];
                }
                // add data onto the end of the key's array
                output[prop].push(item[prop]);
            }
        }
    }
    return output;
}

Working jsFiddle: http://jsfiddle.net/jfriend00/0jjquju9/

Explanation:

  1. For each item in the array
  2. For each key in an item in the array
  3. Add the key as an empty array to the output object if it doesn't already exist
  4. Add the data onto the end of the array for that key in the output object
  5. Return the resulting new object

Upvotes: 8

Barmar
Barmar

Reputation: 782717

result = {};
for (var i = 0; i < array.length; i++) {
    var obj = array[i];
    for (var key in obj) {
        if (obj.hasOwnProperty(key)) {
            if (result[key]) {
                result[key].push(obj[key]);
            } else {
                retult[key] = [obj[key]];
            }
        }
    }
}

Upvotes: 1

Related Questions