0

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}];
3
  • 2
    Well Stackoverflow isn't a free code writing service so it is up to you to show us what you tried so we can help you fix your code Commented Nov 24, 2017 at 5:53
  • Why the objects are enclosed ( & ) instead of [ & ] Commented Nov 24, 2017 at 5:55
  • @brk Because he doesn't know how an array literal looks Commented Nov 24, 2017 at 5:57

3 Answers 3

1

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 );

Sign up to request clarification or add additional context in comments.

Comments

0

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);

Comments

0

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));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.