Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

204
Views
Extracting values out of an array of objects?

I am trying to extract id from the below array of objects and so far I am able to give it a go with the below code but it is showing undefined and cannot get id , would you please check the code and adjust to get id out?

const array = [{
    contact: {
      id: 1,
      email: 'roar@gmail.com',

    },
    date: '2/4/22'
  },
  {
    contact: {
      id: 2,
      email: 'grr@gmail.com',

    },
    date: '2/4/22'
  }
]

function extractValue(arr, prop) {

  let extractedValue = [];

  for (let i = 0; i < arr.length; ++i) {
    // extract value from property
    extractedValue.push(arr[i][prop]);
  }
  return extractedValue;
}


const result = extractValue(array, 'contact.id');
console.log(result);

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

const extractValue = (array, path) => {
  const [first, second] = path.split('.')
  if (second) return array.map(obj => obj[first][second])
  
  return array.map(obj => obj[first])
}

const res = extractValue(array, 'contact.id')
console.log(res)
// => [ 1, 2 ]

this will support single and double level nested results

about 4 years ago · Juan Pablo Isaza Report

0

A good way to do this is the Array Map method

This will get all the id's from your array

const result = array.map((val) => val.contact.id)
about 4 years ago · Juan Pablo Isaza Report

0

function find(val, arr) {  
  for (let x of arr)
    val = val[x];
  return val;  
}
function extractValue(arr, prop) {
 return array.map(x => find(x, prop.split(".")))
}
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!