Home > Enterprise >  How to simplify this piece of code ? Js - React
How to simplify this piece of code ? Js - React

Time:12-18

I need to simplify this function to be more ES6 and more readable. First check my code:

  let datas =  [];
  if (bigArray.length > 0) {
    let newArr = [
      bigArray.map((name) => { 
        if (name) {
          return {
            regex: new RegExp(${name}, 'i'),
            className: 'red-color'
          };
        }
      })
    ];
 
    datas = newArr;
  }

This code is little mass and not readable. Probably can better?

CodePudding user response:

I feel like I could help if I could see more of your code please

CodePudding user response:

You could try something like this:

const createObject = (name) => {
  return {
    regex: new RegExp(`${name}`, "i"),
    className: "red-color",
  };
};

const datas = bigArray.map(createObject);
  • Related