Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to handle the dynamic `RegExp` in the loop

I am trying to group the array by testing them using regex. but getting no result
how this scenario handled?

my reg pattens are there in the object key.

const array = [{
    label: '(GMT) Africa/Asmara',
    name: 'Africa/Asmara',
  },
  {
    label: '(GMT) Monrovia, Reykjavik',
    name: '(GMT) Monrovia, Reykjavik',
  },
  {
    label: '(GMT) Lisbon',
    name: '(GMT) Lisbon',
  },
  {
    label: '(GMT) Greenwich Mean Time : Edinburgh, London',
    name: '(GMT) Greenwich Mean Time : Edinburgh, London',
  },
  {
    label: '(GMT+3:0) Africa/Asmara',
    name: 'Africa/Asmara',
  },

  {
    label: '(GMT+2:0) Africa/Bujumbura',
    name: 'Africa/Bujumbura',
  },

  {
    label: '(GMT+0:0) Africa/Freetown',
    name: 'Africa/Freetown',
  },

  {
    label: '(GMT+11:30) Pacific/Norfolk',
    name: 'Pacific/Norfolk',
  },
  {
    label: '(GMT-11:0) Pacific/Midway',
    name: 'Pacific/Midway',
  },
  {
    label: '(GMT-10:0) Pacific/Rarotonga',
    name: 'Pacific/Rarotonga',
  },

  {
    label: '(GMT-11:30) Pacific/Norfolk',
    name: 'Pacific/Norfolk',
  },
  {
    label: '(GMT-14:0) Pacific/Kiritimati',
    name: 'Pacific/Kiritimati',
  },
  {
    label: '(GMT-12:45) Pacific/Chatham',
    name: 'Pacific/Chatham',
  },
  {
    label: '(GMT-1:0) Europe/Sarajevo',
    name: 'Europe/Sarajevo',
  },
  {
    label: '(GMT-2:0) Atlantic/South_Georgia',
    name: 'Atlantic/South_Georgia',
  },
  {
    label: '(GMT-12:50) Pacific/Chatham',
    name: 'Pacific/Chatham',
  },
];
const storedTypes = {
  '(GMT+': [],
  '(GMT-': [],
  '(GMT': []
};

Object.keys(storedTypes).forEach((t) => {
  array.forEach((v) => {
    const {label} = v;
    if (RegExp(`/${t}/`).test(label)) {
      storedTypes(t).push(v);
    }
  });
});

console.log(storedTypes);

Live Demo

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

const array = [{
    label: '(GMT) Africa/Asmara',
    name: 'Africa/Asmara',
  },
  {
    label: '(GMT) Monrovia, Reykjavik',
    name: '(GMT) Monrovia, Reykjavik',
  },
  {
    label: '(GMT) Lisbon',
    name: '(GMT) Lisbon',
  },
  {
    label: '(GMT) Greenwich Mean Time : Edinburgh, London',
    name: '(GMT) Greenwich Mean Time : Edinburgh, London',
  },
  {
    label: '(GMT+3:0) Africa/Asmara',
    name: 'Africa/Asmara',
  },

  {
    label: '(GMT+2:0) Africa/Bujumbura',
    name: 'Africa/Bujumbura',
  },

  {
    label: '(GMT+0:0) Africa/Freetown',
    name: 'Africa/Freetown',
  },

  {
    label: '(GMT+11:30) Pacific/Norfolk',
    name: 'Pacific/Norfolk',
  },
  {
    label: '(GMT-11:0) Pacific/Midway',
    name: 'Pacific/Midway',
  },
  {
    label: '(GMT-10:0) Pacific/Rarotonga',
    name: 'Pacific/Rarotonga',
  },

  {
    label: '(GMT-11:30) Pacific/Norfolk',
    name: 'Pacific/Norfolk',
  },
  {
    label: '(GMT-14:0) Pacific/Kiritimati',
    name: 'Pacific/Kiritimati',
  },
  {
    label: '(GMT-12:45) Pacific/Chatham',
    name: 'Pacific/Chatham',
  },
  {
    label: '(GMT-1:0) Europe/Sarajevo',
    name: 'Europe/Sarajevo',
  },
  {
    label: '(GMT-2:0) Atlantic/South_Georgia',
    name: 'Atlantic/South_Georgia',
  },
  {
    label: '(GMT-12:50) Pacific/Chatham',
    name: 'Pacific/Chatham',
  },
];
const storedTypes = {
  '(GMT\\+': [],
  '(GMT-': [],
  '(GMT': []
};

Object.keys(storedTypes).forEach(t => {
  const exp = new RegExp(`\\${t}`);
  array.forEach(v => {
    const {label} = v;
    if (exp.test(label)) { 
      storedTypes[t].push(v);
    }
  });
});

console.log(storedTypes);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading