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

116
Views
function map in Javascript

I am studying Javascript and trying to make the Map function print only the name. I try to do that but I did not succeed. I hope to find a solution for you. Thank you

const x = [{
  a: {
    name: "X",
    age: "25"
  },
  b: {
    name: "Y",
    age: "30"
  }
}]

Object.values(x).map((el) => {

  console.log(Object.values(el).map((el2) => {
    el2
  }))

})

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

0

You don't return any values in your second map.

Return the name and it's would work :

const x = [{
  a: {
    name: "X",
    age: "25"
  },
  b: {
    name: "Y",
    age: "30"
  }
}]

x.forEach((el) => {
  console.log(Object.values(el).map(({ name }) => name))
})

Note: replace your first map by forEach, is more appropriate

about 4 years ago · Juan Pablo Isaza Report

0

.map() is to map the data into a new array. You need .forEach().

Since you have an array of objects. And each object has multiple keys, you can use Object.keys() for the inner objects.

const x = [{
  a: {
    name: "X",
    age: "25"
  },
  b: {
    name: "Y",
    age: "30"
  }
}]

x.forEach((el) => {
  let keys = Object.keys(el);
  //console.log(keys);
  keys.forEach((innerKey) => {
     console.log(el[innerKey].name);
  });

})

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!