Skip to content
Advertisement

fetch specifi data from json and store them in array using node js / Javascript

I am trying to fetch all the log details from this json object but the problem is this key always get changed

let userJsonObject =
{
  "user_1":{
     "id":"user_1",
     "log":"de-data",
     "name":"dummy test",
     "address":"New York"
  },
  "user_2":{
     "id":"user_2",
     "log":"ex-file",
     "name":"tom",
     "address":"Nevada"
  },
  "user_3":{
     "id":"user_3",
     "log":"dis-acta",
     "name":"Jerry",
     "address":"LA"
  },
}

this is my json Object and I want fetch all the log which are present inside the json object and store them in one array like this

let gotLog= ["de-data","ex-file","dis-acta"]

here user_1 user_2 get changed everytime and I wanted to tackle this scenario and fetch all the log in one array

Advertisement

Answer

let userJsonObject =
{
  "user_1":{
     "id":"user_1",
     "log":"de-data",
     "name":"dummy test",
     "address":"New York"
  },
  "user_2":{
     "id":"user_2",
     "log":"ex-file",
     "name":"tom",
     "address":"Nevada"
  },
  "user_3":{
     "id":"user_3",
     "log":"dis-acta",
     "name":"Jerry",
     "address":"LA"
  },
};


let array=[];
for(const value of Object.values(userJsonObject)){
    array.push(value.log)
};
console.log(array);

I got it 🙂

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement