0

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

1 Answer 1

1

That else branch is inside the inner array, however you want to push a 0-entry only if the inner loop has not found the given date in result, which you only know when the loop is over (new variable found tracks it):

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) {
  var found=false;
  for (keyResult in result) {
    if (dateList[key] === result[keyResult].CDate) {
      var obj = {
        "date": dateList[key],
        "value": result[keyResult].executions
      }
      allList.push(obj);
      found=true;
      break;
    }// else {
  }
  if(!found){
    var obj = {
      "date": dateList[key],
      "value": 0
    }
    allList.push(obj)
    //break;
  }
}
console.log(allList);

An alternative approach would be creating a 0-entry at the beginning an updating it from the inner loop and push at the end:

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) {
  var obj = {
    "date": dateList[key],
    "value": 0
  };
  for (keyResult in result) {
    if (dateList[key] === result[keyResult].CDate) {
      obj.value=result[keyResult].executions;
      break;
    }
  }
  allList.push(obj)
}
console.log(allList)

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

1 Comment

Above two methods working fine,In second method logic very nice.Thanks.:)

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.