0

I have an object of objects in which I want to print all the name values, how can I do this easily without having to hardcode each one?

const myObj = {
  results: {
      one: {
         name: "o", 
         value: 1
      } 
      two: {
         name: "t", 
         value: 2
      }
    }
   ignore: "this"
} 

I can make it work with myObj.results.one.name, but I'd rather be able to do something like a loop where I can swap out the middle value like results.[1].name preventing the hard coding of 20 values.

Is there a simple way to do this without hardcoding each one. (The end use is to add them all to an array and use that to populate a table to show name and values)

This linked answer doesn't answer my question as this is an array of objects so solutions fail, and the second isn't nested

Uciebila
  • 481
  • 2
  • 9
  • 27
  • What is your expected result? – DecPK Jun 21 '21 at 15:00
  • @decpk I'm wanting to get an output of just the name values, so like "o, t" – Uciebila Jun 21 '21 at 15:07
  • Note the linked questions don't properly answer what am asking as their structures are different – Uciebila Jun 21 '21 at 15:38
  • Neither links help but I did find a simple solution I can add if the q gets reopened – Uciebila Jun 21 '21 at 16:45
  • If you want `output` in as an array then use `Object.values(myObj.results).map((o) => o.name);` . If you want output in string then use `Object.values(myObj.results) .map((o) => o.name) .join(", ");` – DecPK Jun 21 '21 at 22:15

0 Answers0