1

I have an array with object like this

var arr = [
  {'key1':'A','key2':1,'key3':0.04,'key4':10},
  {'key1':'B','key2':1,'key3':0.03,'key4':20},
  {'key1':'B','key2':2,'key3':0.05,'key4':30},
  {'key1':'A','key2':1,'key3':0.03,'key4':40},
  ...
];

Inside array there can be unlimited objects. I need to create new array where every key1 and key2 must save unique and if same key1 and key2,then key3 must be added. My result for above array look like this

//Can be array or object
var a = [
  ['A'][1] => {'key3':0.07,'key4':50},
  ['B'][1] => {'key3':0.07,'key4':20},
  ['B'][2] => {'key3':0.05,'key4':30}
];

My result can be array or object. What I have tried is

var a = {};
arr.forEach(function(obj){
  if(a[obj.key1]){
    if(a[obj.key1][obj.key2]){
      a[obj.key1][obj.key2] += obj.key3;
    }else{
      a[obj.key1][obj.key2] = obj.key3;
    }
  }else{
    a[obj.key1] = obj.key1;
  }
});
console.log(a);

And I am getting only depth of result. Please suggest me how can I get my result output from given array object. Thanks in advance.

1
  • sorry, my mistake.Please check the edit. Commented Mar 29, 2017 at 9:18

2 Answers 2

2

For more nested objects, you could use an array for grouping the keys and one for the keys with the value to add. Then build a new object, if necessary and assign the values.

var array = [{ key1: 'A', key2: 1, key3: 0.04, key4: 10 }, { key1: 'B', key2: 1, key3: 0.03, key4: 20 }, { key1: 'B', key2: 2, key3: 0.05, key4: 30 }, { key1: 'A', key2: 1, key3: 0.03, key4: 40 }],
    groupKeys = ['key1', 'key2'],
    valueKeys = ['key3', 'key4'],
    result = Object.create(null);

array.forEach(function (a) {
    var object = groupKeys.reduce(function (r, k) {
        return r[a[k]] = r[a[k]] || {};
    }, result);
    valueKeys.forEach(function (k) {
        object[k] = (object[k] || 0) + a[k];
    });
});

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

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

3 Comments

if i need to add more value inside like ['A'][1][0.07]=> 4,How can i achieve this.Is works but I don't get how its work.Thank You.
It works and Thanks. But I prefer before one solution without define key.
where do you know, which keys is a grouping key and which is for the value?
0
var arr = [
    {'key1':'A','key2':1,'key3':0.04},
    {'key1':'B','key2':1,'key3':0.03},
    {'key1':'B','key2':2,'key3':0.05},
    {'key1':'A','key2':1,'key3':0.03}
];
var tempArr = $.extend(true, [], arr);
var newArray = new Array();
var len = arr.length;
for(var i=0;i<len;i++){
    var totalk3 = arr[i].key3;

    for(var j=i+1;j<tempArr.length;j++){

        if(arr[i].key1 == tempArr[j].key1 && arr[i].key2 == tempArr[j].key2){
            totalk3 = totalk3 + tempArr[j].key3;
            arr.splice(j,1);
        }
    }

    var json1 = {};
    var json2 = {};
    json2[arr[i].key2] = totalk3;
    json1[arr[i].key1]=json2;
    newArray[i] = json1;

    len = arr.length;
}
console.log(newArray);

2 Comments

please explain why this answers the question
Iterated with two loops the outer one is for all individual JSON object for main Array [arr] and inner one is for addition of key3's from json's where key1 and key2 is same in any case and after addition will remove that [arr] json from outer loop and its length is also updated.

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.