DEV Community

baltz
baltz

Posted on

Quick tip! Map loop, already returns a array

It is common to see this approach when we are creating a new array using map loop

const newArray = []

array.map((item) => {
  newArray.push(item.value)
})
Enter fullscreen mode Exit fullscreen mode

Right? but map loop already returns a array.
So we can do that instead

const newArray = array.map((item) => {
  return item.value
})

Enter fullscreen mode Exit fullscreen mode

Cool huh!
Cool huh!

Top comments (0)