I want to compare array and array of objects in node.js and generate a new json objects based on comparing. In the below code i am having "dateList" list of date and result having array of json, Now i want to compare both dateList and result, if result object having the date of dateList then we have to get the executions value and push into new array if not equal then we have to push as 0.
Server code:
var dateList = [
"2018-08-01",
"2018-07-31",
"2018-07-30",
"2018-07-29",
"2018-07-28",
"2018-07-27",
"2018-07-26"
]
var result = [{
CDate: '2018-07-31',
executions: 1
},
{
CDate: '2018-07-30',
executions: 2
},
{
CDate: '2018-07-27',
executions: 3
},
{
CDate: '2018-07-26',
executions: 2
}
];
var allList = [];
for (key in dateList) {
for (keyResult in result) {
if (dateList[key] === result[keyResult].CDate) {
var obj = {
"date": dateList[key],
"value": result[keyResult].executions
}
allList.push(obj);
break;
} else {
var obj = {
"date": dateList[key],
"value": 0
}
allList.push(obj)
break;
}
}
}
console.log(allList);
Current Output:
[{
"date": "2018-08-01",
"value": 0
},
{
"date": "2018-07-31",
"value": 1
},
{
"date": "2018-07-30",
"value": 0
},
{
"date": "2018-07-29",
"value": 0
},
{
"date": "2018-07-28",
"value": 0
},
{
"date": "2018-07-27",
"value": 0
},
{
"date": "2018-07-26",
"value": 0
}
]
Expected Output:
[{
"date": "2018-08-01",
"value": 0
},
{
"date": "2018-07-31",
"value": 1
},
{
"date": "2018-07-30",
"value": 2
},
{
"date": "2018-07-29",
"value": 0
},
{
"date": "2018-07-28",
"value": 0
},
{
"date": "2018-07-27",
"value": 3
},
{
"date": "2018-07-26",
"value": 2
}
]
Kindly help