DEV Community

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

Posted on • Edited on

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

Top comments (0)