Home > Back-end >  fetch specifi data from json and store them in array using node js / Javascript
fetch specifi data from json and store them in array using node js / Javascript

Time:06-08

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

CodePudding user response:

var obj = { first: 'someVal' };
obj[Object.keys(obj)[0]];

I found this here How to access the first property of a Javascript object?

CodePudding user response:

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 :)

  • Related