I have two arrays and I am trying to compare them in order to find the matching values only.
For instance, both arrays contain the date 2023/03/07
, which is the value that should be returned.
I haven't come across a solution for this yet. What would be the most efficient way to identify the similarities?
var array1 = ["2023/03/07", "2023/03/17", "2023/03/27"];
var array2 = [];
var startDate = "2023/03/05";
var endDate = "2023/03/15";
var dateMove = new Date(startDate);
var strDate = startDate;
while (strDate < endDate) {
var strDate = dateMove.toISOString().slice(0, 10);
array2.push(strDate);
dateMove.setDate(dateMove.getDate() + 1);
}
found = array1.find((val, index) => {
return array2.includes(val);
});
console.log(found);
// if value in array1 is equal to value in array2 then return match: 2023/03/07