DEV Community

Cover image for Ways to use map() function
Dezina
Dezina

Posted on • Edited on

3 3

Ways to use map() function

to return more than one value

let desig
    let reqId
    const searchRow = data.map(opts => {
      desig = opts.value.split(' - ')[0]
      reqId = opts.value.split(' - ')[1]
    })
    console.log("desig", desig)
    console.log("reqId", reqId)
Enter fullscreen mode Exit fullscreen mode

to return one value

const objectIds = jobIds.map(id => id._id)
console.log("objectIds", objectIds)
Enter fullscreen mode Exit fullscreen mode

to return arrays

const corporateOptions = result.map(opt => {
        opt.Candidate.map(desig => {

          desigArray.push(desig.cDesig)
        })
        opt.Jobdata.map(reqid => {
          reqIdArray.push(reqid.reqId)
        })
      })
Enter fullscreen mode Exit fullscreen mode

to return object arrays

 let result = []
        filterIntreviews.map((ob) => {
          let createObj = {
            id: ob.id,
            cName: ob.cName,
            reqId: ob.reqId,
            desig: ob.desig
          }
          result.push(createObj)
         })

        console.log("result", result)
Enter fullscreen mode Exit fullscreen mode

To create an array from array object

counterArray [
  { counter: 1 },
  { counter: 2 },
  { counter: 3 }
]
const counters = counterArray.map(ct => ct.counter)
console.log(counters)
output will be => [1, 2, 3]
Enter fullscreen mode Exit fullscreen mode

Heroku

Simplify your DevOps and maximize your time.

Since 2007, Heroku has been the go-to platform for developers as it monitors uptime, performance, and infrastructure concerns, allowing you to focus on writing code.

Learn More

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay