I am facing a challenge where I have two JSON arrays, each containing a field named date
. My goal is to compare the two arrays and merge them into a single array. Check out the code snippet below:
var firstArr=[{'name':'Ram','date':'2017-12-06'},{'name':'Raja','date':'2017-12-07'},{'name':'Rahul','date':'2017-12-08'}];
var secondArr=[{'model':'mmm','date':'2017-12-06'},{'model':'rrr','date':'2017-12-09'}];
In this scenario, I need to analyze both arrays based on their dates and combine their values into one unified array. The desired output should look like this:
var finalArr=[{'name':'Ram','date':'2017-12-06','model':'mmm'},{'name':'Raja','date':'2017-12-07','model':''},{'name':'Rahul','date':'2017-12-08','model':''},{'name':'','date':'2017-12-09','model':'rrr'}]
The expected outcome has been provided above. Currently, my attempt to achieve this looks like as follows.
angular.forEach(firstArr,function(obj1){
angular.forEach(secondArr.task,function(obj2){
if (new Date(obj1.date)=== new Date(obj2.date)) {
}
})
})
However, I am encountering some confusion regarding the different lengths of the two arrays, as they may or may not be equal.