Home > database >  Is possible to set map loop in one line code? Javascript Map function
Is possible to set map loop in one line code? Javascript Map function

Time:06-15

I want to try to simpilfy map in one row... Check my loop

   arrayData.map((arr) => {
      if (checkName(arr.name)) {
        console.log("TRUE");
    }
  }); 

CodePudding user response:

Array.map is used when you want to create a new array from another array. you can make a one-liner indeed ! Here's how I would do it:

arrayData.forEach((item) => checkName(item.name) && console.log("TRUE"));

CodePudding user response:

You can use && chain with function implementation.

arrayData.map((arr) => checkName(arr.name) && console.log("TRUE"))

It will show the console log for what you want. But map function is purposed to get a new array, you can use other array operators such as forEach.

  • Related