Home > front end >  How to handle the dynamic `RegExp` in the loop
How to handle the dynamic `RegExp` in the loop

Time:12-28

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

CodePudding user response:

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

CodePudding user response:

You can add .startsWith() or .includes() in here. Regexp is just going to make it more and more hectic.

CodePudding user response:

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 = array.reduce((acc, v) => {
  let label = v.label.split(' ')[0];
  if (/^\(GMT\)/.test(label)) {
    acc['(GMT)'].push(v);
  } else if (/^\(GMT\ /.test(label)) {
    acc['(GMT )'].push(v);
  } else if (/^\(GMT\-/.test(label)) {
    acc['(GMT-)'].push(v);
  }
  return acc;
}, {
  '(GMT )': [],
  '(GMT-)': [],
  '(GMT)': []
});

console.log(storedTypes);

CodePudding user response:

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

  • Related