Reputation: 35
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
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
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
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:
Upvotes: 8
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