Home > Mobile >  Looking for a way to iterate through an array of dictionaries to see if a particular substring is in
Looking for a way to iterate through an array of dictionaries to see if a particular substring is in

Time:09-21

So I'm just needing to return a value as true/false for a list of dictionaries.

I need to check if the date key in these dictionaries contains currentDate and then flag as true.

This will be used in conjunction with moment-timezones to check if it's a public holiday so my work can turn off our phone lines.

Example is:

const currentDate = '2022-01-26';

arr = 
[
 {
   date: '2022-01-01 00:00:00',
   start: 2021-12-31T13:00:00.000Z,
   end: 2022-01-01T13:00:00.000Z,
   name: "New Year's Day",
   type: 'public',
   rule: '01-01 and if saturday,sunday then next monday'
 },
 {
   date: '2022-01-03 00:00:00',
   start: 2022-01-02T13:00:00.000Z,
   end: 2022-01-03T13:00:00.000Z,
   name: "New Year's Day",
   type: 'public',
   rule: '01-01 and if saturday,sunday then next monday'
 },
 {
   date: '2022-01-26 00:00:00',
   start: 2022-01-25T13:00:00.000Z,
   end: 2022-01-26T13:00:00.000Z,
   name: 'Australia Day',
   type: 'public',
   rule: '01-26 if saturday,sunday then next monday'
 }
] 

Closest I've got is using:

holiday.forEach(function(d){
    console.log(d.date.includes(currentDate));

});

Which in this example would return:

false
false
true

I'm struggling to convert this into an 'if' statement when if one value is true I can perform another action.

Thanks in advance.

CodePudding user response:

You can use filter method for it

const currentDate = '2022-01-26';

let arr = 
[
 {
   date: '2022-01-01 00:00:00',

   name: "New Year's Day",
   type: 'public',
   rule: '01-01 and if saturday,sunday then next monday'
 },
 {
   date: '2022-01-03 00:00:00',
  
   name: "New Year's Day",
   type: 'public',
   rule: '01-01 and if saturday,sunday then next monday'
 },
 {
   date: '2022-01-26 00:00:00',
  
   name: 'Australia Day',
   type: 'public',
   rule: '01-26 if saturday,sunday then next monday'
 }
] 
const result = arr.filter(item=> item.date.includes(currentDate));
console.log(result)
if (result.length === 1){
// turn off the lines
} 

CodePudding user response:

Sure you can just use map to iterate over your data and build a result array. e.g.

const currentDate = '2022-01-26';

const data = [{
    date: '2022-01-01 00:00:00',
    start: '2021-12-31T13:00:00.000Z',
    end: '2022-01-01T13:00:00.000Z',
    name: "New Year's Day",
    type: 'public',
    rule: '01-01 and if saturday,sunday then next monday'
  },
  {
    date: '2022-01-03 00:00:00',
    start: '2022-01-02T13:00:00.000Z',
    end: '2022-01-03T13:00:00.000Z',
    name: "New Year's Day",
    type: 'public',
    rule: '01-01 and if saturday,sunday then next monday'
  },
  {
    date: '2022-01-26 00:00:00',
    start: '2022-01-25T13:00:00.000Z',
    end: '2022-01-26T13:00:00.000Z',
    name: 'Australia Day',
    type: 'public',
    rule: '01-26 if saturday,sunday then next monday'
  }
];

const result = data.map(x => x.date.startsWith(currentDate));

console.log(result);

CodePudding user response:

const currentDate = '2022-01-26';

const holidays = [{
 date: '2022-01-01 00:00:00'
}, {
 date: '2022-01-03 00:00:00'
}, {
 date: '2022-01-26 00:00:00'
}];

function isHoliday(dateStr) {
  return holidays.some(holiday =>
    holiday.date.includes(dateStr)
  );
}

console.log(isHoliday(currentDate));

  • Related