var test=[];
$(document).ready(function(){
$.getJSON("data.json",function(data){
$.each(data,function(key,value){
test.push(value.topic);
});
});
});
I have written some javascript code to push all values of a json object with the key as topic into an array called test. However, when I try to access test[i] (where i is any integer within the array length), I receive an error saying "undefined". Can you help me figure out what I am missing?
Below is a sample of my json object:
[
{
"topic": "Books",
"quantity": 3
},
{
"topic": "Grossery",
"quantity": 3
},
{
"topic": "stationery",
"quantity": 3
},
{
"topic": "food",
"quantity": 2
},
{
"topic": "market items",
"quantity": 3
}
]