Skip to content
Advertisement

Parse two arrays to check each value JavaScript

So i have two lists.

List A contains a list of all dates of a specified month. list B contains a sequence of 1 and 0´s. If i want to check if date from list A is equal to 1 or 0 corresponding to the position in list B, How should i approach this?.

The idea is to check if day 1,2,3.. and so on is value 1 or 0 from List B..

Example 2020-02-01 = 1 or 0…

var listDate = [];
var startDate ='2020-02-01';
var endDate = '2020-02-29';
var dateMove = new Date(startDate);
var strDate = startDate;

while (strDate < endDate){
  var strDate = dateMove.toISOString().slice(0,10);
  var dayCount = listDate.length;
  listDate.push(strDate);
  dateMove.setDate(dateMove.getDate()+1);
};
console.log(dayCount + 1)
console.log(listDate)

Then i have another list that contains a sequence of 1 and 0 ´s.

var str = "1100000110000011000001100000100";
var res = str.split("");
var n = str.length;

console.log(n)
console.log(res)

Advertisement

Answer

Like this?

const res = "1100000110000011000001100000100".split("");


var listDate = [];
var startDate ='2020-02-01';
var endDate = '2020-02-29';
var dateMove = new Date(startDate);
var strDate = startDate;

while (strDate < endDate){
  var strDate = dateMove.toISOString().slice(0,10);
  var dayCount = listDate.length;
  listDate.push(strDate);
  dateMove.setDate(dateMove.getDate()+1);
};

// code based on day number rather than the place in the array

listDate.forEach(dt => console.log(dt,res[dt.split("-")[2]-1]))
let weekdays = listDate.filter(dt => res[dt.split("-")[2]-1]==="0")
let weekends = listDate.filter(dt => res[dt.split("-")[2]-1]==="1")
console.log(weekends)

// same code based on index

listDate.forEach((dt,i) => console.log(dt,res[i]))
weekdays = listDate.filter((dt,i) => res[i]==="0")
weekends = listDate.filter((dt,i) => res[i]==="1")
console.log(weekends)
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement