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
  }]
}]

4
  • 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; } Commented Apr 6, 2015 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 tinyurl.com/l92nboe then inside the loop with data2 tinyurl.com/k5duy6l Commented Apr 6, 2015 at 11:21
  • Why not use underscore.js Commented Apr 6, 2015 at 11:33
  • @kirnp Please check my answer below with the working code. Commented Apr 6, 2015 at 11:51

3 Answers 3

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

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

Comments

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;

1 Comment

That's as close as you can get to a merge.
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/)

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.