0

How to merge the two array of objects using javascript for given data.

var data = [{
  "key": 2000,
  "value": 10000
}, {
  "key": 2001,
  "value": 50000
}];

var data1 = [{
  "key": [2000, 0],
  "value": 1000
}, {
  "key": [2000, 1],
  "value": 1500
}, {
  "key": [2000, 3],
  "value": 2000
}, {
  "key": [2001, 0],
  "value": 2500
}, {
  "key": [2001, 1],
  "value": 3000
}, {
  "key": [2001, 3],
  "value": 4000
}];

And finally out put is given below:

var data3 = [{
  "key": 2000,
  "value": 10000,
  children: [{
    "key": [2000, 0],
    "value": 1000
  }, {
    "key": [2000, 1],
    "value": 1500
  }, {
    "key": [2000, 3],
    "value": 2000
  }]
}, {
  "key": 2001,
  "value": 50000,
  children: [{
    "key": [2001, 0],
    "value": 2500
  }, {
    "key": [2001, 1],
    "value": 3000
  }, {
    "key": [2001, 3],
    "value": 4000
  }]
}]
Ejaz
  • 8,719
  • 3
  • 34
  • 49
kirn p
  • 25
  • 1
  • I am tried for the given code: function fctnCombine(){var arr=[]; for(var i in data1){ var year;var obj={}; if(year==2009){ obj.key=new Date(data1[i].key[0],data1[i].key[1]); obj.value=data1[i].value; // console.log(obj) arr.push(obj); } } return arr; } – kirn p Apr 06 '15 at 11:19
  • 1
    I will not be the one who will steal to you the pleasure to found a nice algorithm. I will just give you a hint: first with data http://tinyurl.com/l92nboe then inside the loop with data2 http://tinyurl.com/k5duy6l – user688877 Apr 06 '15 at 11:21
  • Why not use underscore.js – wonderbell Apr 06 '15 at 11:33
  • @kirnp Please check my answer below with the working code. – Rahul Desai Apr 06 '15 at 11:51

3 Answers3

1

This iterates as many times as there are objects in the array data1.

Then, in each iteration it will add the object (equal to the variable count) in data1 to data.

Afterwards, it will make the variable data1 undefined.

for(var count = 0; count < data1.length; count++){
    data.push(data1[count]);
}
data1 = undefined;
Oliver
  • 1,576
  • 1
  • 17
  • 31
1

You can achieve this by iterating over the given 2 arrays using forEach loops.

Working code snippet:

var data = [{
  "key": 2000,
  "value": 10000
}, {
  "key": 2001,
  "value": 50000
}];

var data1 = [{
  "key": [2000, 0],
  "value": 1000
}, {
  "key": [2000, 1],
  "value": 1500
}, {
  "key": [2000, 3],
  "value": 2000
}, {
  "key": [2001, 0],
  "value": 2500
}, {
  "key": [2001, 1],
  "value": 3000
}, {
  "key": [2001, 3],
  "value": 4000
}];

data.forEach(function(parentObj){  // loop over data
  
  parentObj.children = [];  // initialize the children
  
  data1.forEach(function(childObj){  // loop over data1
    
    if(parentObj["key"] === childObj["key"][0])
      parentObj.children.push(childObj);
    
  });
  
});

console.dir(data);
<p>Please check your browser's console now.</p>

Source

Read up: Array.prototype.forEach() | MDN

Community
  • 1
  • 1
Rahul Desai
  • 15,242
  • 19
  • 83
  • 138
0

It is multidimensional array. Please check the demo link as follows. This is demo link click enter code here[here] (http://jsfiddle.net/ambiguous/twpUF/)

Deep Kakkar
  • 5,831
  • 4
  • 39
  • 75