Reputation: 25
I have problem to get output with this JSON
{
status: "ok",
query:
{
format: "JSON",
city: "667",
date: "2020-10-14"
},
schedule:
{
status: "ok",
data:
{
prayer1: "14:46",
prayer2: "05:55",
prayer3: "11:42",
prayer4: "04:06",
prayer5: "18:58",
prayer6: "17:48",
prayer7: "04:16",
prayer8: "Rabu, 14 Oct 2020",
prayer9: "05:29"
}
}
}
I want to get specific data key or value (exp. prayer1
)
I tried this code
Object.keys(JSON).forEach(function (key) {
console.log(JSON[key].data);
});
But it just outputs all the data, not a specific data
Upvotes: 1
Views: 47
Reputation: 44182
There's no need to use a forEach()
if you want to get a data from a specific key (eg; prayer1
).
Example;
const data = { status: "ok", query: {format: "JSON", city: "667", date: "2020-10-14"}, schedule: {status: "ok", data: {prayer1: "14:46", prayer2: "05:55", prayer3: "11:42", prayer4: "04:06", prayer5: "18:58", prayer6: "17:48", prayer7: "04:16", prayer8: "Rabu, 14 Oct 2020", prayer9: "05:29"} } }
// To seach
const getKey = 'prayer1';
// Since we only need data.schedule.data
const searchIn = data.schedule.data;
// Get value by key
const value = searchIn[getKey];
console.log(value);
Upvotes: 0
Reputation: 10675
let json = {
status: "ok",
query: {
format: "JSON",
city: "667",
date: "2020-10-14"
},
schedule: {
status: "ok",
data: {
prayer1: "14:46",
prayer2: "05:55",
prayer3: "11:42",
prayer4: "04:06",
prayer5: "18:58",
prayer6: "17:48",
prayer7: "04:16",
prayer8: "Rabu, 14 Oct 2020",
prayer9: "05:29"
}
}
}
Object.keys(json.schedule.data).forEach(key => {
console.log(json.schedule.data[key])
})
/*
OUTPUT:
14:46
05:55
11:42
04:06
18:58
17:48
04:16
Rabu, 14 Oct 2020
05:29
*/
Repl.it: https://repl.it/join/atkidrbj-theketan2
Upvotes: 1