Say I have JSON data as below:
var db=[
{"name":"John Doe","age":25},
{"name":"John Doe 2","age":21}
]
I know how to retrieve data from the JSON object using js/jquery. What I need is to get the value of the labels, i.e. I want to retrieve the "name" and the "age" from the object. I don't actually want to retrieve the values "John Doe" and 25 (I can already do that), instead I want the labels.
Say the JSON object has about 5 data with 10 fields each. I need to display the JSON in table form dynamically which I can already do. Since the JSON may be any set of values, I won't know what label to put in the <th></th>
cells of the table. So I need a way to get the labels.
Using a pseudo code:
var db=[
{"name":"John Doe","age":25},
{"name":"John Doe 2","age":21}
]
for i in db{
for j in db[i]{
console.log(db[i][j].label+":"db[i][j])
}
}
//db[i][j].label doesn't really work
The output should be:
name: John Doe
age: 25
name: John Doe
age: 21
Is there a JavaScript or jQuery function to do so or some other method to retrieve the required data?
You're already fetching the property name (which you are calling "label"). You need it to get the property's value out of the object.
So, it's just j
.
console.log(j + ":" + db[i][j]);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With