简体   繁体   中英

Looking for a way to iterate through an array of dictionaries to see if a particular substring is inside a value -- Javascript

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.

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 }

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

 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);

 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));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM