Sz4
Sz4

Reputation: 27

Convert an array of objects to another array of objects using jQuery

I am getting a result in my JavaScript file which I want to convert into another object.

My original result:

 results = [{Key1:'Value1', Key2:100, Key3:'A'},
            {Key1:'Value1', Key2:40,  Key3:'B'},
            {Key1:'Value2', Key2:60,  Key3:'A'},
            {Key1:'Value2', Key2:70,  Key3:'B'},
            {Key1:'Value3', Key2:50,  Key3:'A'},
            {Key1:'Value3', Key2:90,  Key3:'B'}];

Convert this to look like an array of objects using jQuery or JavaScript. How can I achieve this?

finalResult=[{Key1:'Value1', A:100, B:40},
             {Key1:'Value2', A:60,  B:70},
             {Key1:'Value3', A:50,  B:90}];

Upvotes: 0

Views: 773

Answers (3)

Hassan Imam
Hassan Imam

Reputation: 22534

You can use array#reduce to group objects based on the Key1 value. Then using Object.values(), you can get the accumulated result of each key.

const input = [{Key1:'Value1',Key2:100,Key3:'A'},{Key1:'Value1',Key2:40,Key3:'B'},{Key1:'Value2',Key2:60,Key3:'A'},{Key1:'Value2',Key2:70,Key3:'B'},{Key1:'Value3',Key2:50,Key3:'A'},{Key1:'Value3',Key2:90,Key3:'B'}];

const result = input.reduce((r, {Key1, Key2, Key3}) => {
  r[Key1] ?
      r[Key1][Key3] = Key2
    : r[Key1] = {Key1, [Key3] : Key2 };
  return r;
},{});
console.log(Object.values(result));

Upvotes: 0

JBoothUA
JBoothUA

Reputation: 3149

Use this:

let results=[{Key1:'Value1',Key2:100,Key3:'A'},
         {Key1:'Value1',Key2:40,Key3:'B'},
         {Key1:'Value2',Key2:60,Key3:'A'},
         {Key1:'Value2',Key2:70,Key3:'B'},
         {Key1:'Value3',Key2:50,Key3:'A'},
         {Key1:'Value3',Key2:90,Key3:'B'}];
         
let finalResult = [];
for (let original of results) {  
    if (!finalResult[original.Key1]) {
        finalResult[original.Key1] = { Key1: original.Key1 };
    }

    finalResult[original.Key1][original.Key3] = original.Key2;
}

console.log(finalResult);

Upvotes: 0

gurvinder372
gurvinder372

Reputation: 68393

You are initializing the array incorrectly, and you need to replace the outer () by [], like

var results = [ {}, {} ];

You need to first make a map of different Key1's

var map = {};
results.forEach( function(item){
   map[ item.Key1 ] = map[ item.Key1 ] || {};
   map[ item.Key1 ][ item.Key3 ] = map[ item.Key1 ][ item.Key3 ] || 0;
   map[ item.Key1 ][ item.Key3 ] += item.Key2;
})

Now iterate the keys of this map, and prepare your output

var output = Object.keys(map).map( function(value){
    var obj = { key1 : value };
    Object.keys( map[ value ] ).forEach( function(key2){
        obj[ key2 ] = map[ value ][ key2 ]
    });
    return obj;
});

Demo

var results=[
  {Key1:'Value1',Key2:100,Key3:'A'},
  {Key1:'Value1',Key2:40,Key3:'B'},
  {Key1:'Value2',Key2:60,Key3:'A'},
  {Key1:'Value2',Key2:70,Key3:'B'},
  {Key1:'Value3',Key2:50,Key3:'A'},
  {Key1:'Value3',Key2:90,Key3:'B'}
];
var map = {};
results.forEach( function(item){
   map[ item.Key1 ] = map[ item.Key1 ] || {};
   map[ item.Key1 ][ item.Key3 ] = map[ item.Key1 ][ item.Key3 ] || 0;
   map[ item.Key1 ][ item.Key3 ] += item.Key2;
});

var output = Object.keys(map).map( function(value){
    var obj = { key1 : value };
    Object.keys( map[ value ] ).forEach( function(key2){
        obj[ key2 ] = map[ value ][ key2 ]
    });
    return obj;
});
console.log( output );

Upvotes: 1

Related Questions