0

I am new to node and I am trying to fetch name from the obj but the problem is

How to fetch data from inner json object as my json object look like this

let obj= 
{
  "h4354desdfqw":{
   name:"Computer",
   os:"Window",
  },
  "hjsado24334":{
   name:"Software",
   type:"Adobe",
  },
  "qwsak032142":{
   name:"hardware",
   type:"hardisk",
  },
}

console.log(obj.h4354desdfqw.name)

I am trying to fetch all the name which are present inside the json object like this

computer
Software
hardware
  • 1
    `Object.values(obj)` and `map` will help – cmgchess Mar 17 '22 at 11:24
  • There's no [JSON](https://www.json.org/json-en.html) in your question: [What is the difference between JSON and Object Literal Notation?](https://stackoverflow.com/questions/2904131/what-is-the-difference-between-json-and-object-literal-notation), and [there's no such thing as a "JSON Object"](http://benalman.com/news/2010/03/theres-no-such-thing-as-a-json/) – Andreas Mar 17 '22 at 11:31

2 Answers2

1

I am not sure in which representation you would like get the data. I can assume - you would like to get array of the names

let obj= 
{
  "h4354desdfqw":{
   name:"Computer",
   os:"Window",
  },
  "hjsado24334":{
   name:"Software",
   type:"Adobe",
  },
  "qwsak032142":{
   name:"hardware",
   type:"hardisk",
  },
}

const result = Object.values(obj).map(i => i.name);

console.log(result)

Object.values(obj).map(i => console.log(i.name));
shutsman
  • 2,357
  • 1
  • 12
  • 23
  • can I get output without square bracket? @shutsman –  Mar 17 '22 at 11:46
  • yes, we can, but these strings should be stored somewhere and array is the best way, we can simply log on each iteration – shutsman Mar 17 '22 at 11:56
0

You want to get the value of name for each object. So let's iterate through each object's name with the map function :

const result = Object.values(obj).map(i => i.name);
console.log(result);
Dennis .VN
  • 46
  • 11