I am trying to fetch all the log details from this json object but the problem is this key always get changed
JavaScript
x
22
22
1
let userJsonObject =
2
{
3
"user_1":{
4
"id":"user_1",
5
"log":"de-data",
6
"name":"dummy test",
7
"address":"New York"
8
},
9
"user_2":{
10
"id":"user_2",
11
"log":"ex-file",
12
"name":"tom",
13
"address":"Nevada"
14
},
15
"user_3":{
16
"id":"user_3",
17
"log":"dis-acta",
18
"name":"Jerry",
19
"address":"LA"
20
},
21
}
22
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
JavaScript
1
2
1
let gotLog= ["de-data","ex-file","dis-acta"]
2
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
JavaScript
1
28
28
1
let userJsonObject =
2
{
3
"user_1":{
4
"id":"user_1",
5
"log":"de-data",
6
"name":"dummy test",
7
"address":"New York"
8
},
9
"user_2":{
10
"id":"user_2",
11
"log":"ex-file",
12
"name":"tom",
13
"address":"Nevada"
14
},
15
"user_3":{
16
"id":"user_3",
17
"log":"dis-acta",
18
"name":"Jerry",
19
"address":"LA"
20
},
21
};
22
23
24
let array=[];
25
for(const value of Object.values(userJsonObject)){
26
array.push(value.log)
27
};
28
console.log(array);
I got it 🙂