Reputation: 245
I'm trying to save certain JSON values to JS array.
var count = Object.keys(item.programme).length; // item is JSON file, count is 23
for (i=0; i<count; i++) {
var title = item.programme[i].title.de;
console.log(typeof title); //string
console.log(title); // desired values, title when i
listData = [];
listData[i] = title;
}
console.log(listData); // [undefined, undefined,.....,title when i =22]
I would like to get array of values from title variable. I get desired value only in last field of array, rest is undefined
.
Upvotes: 1
Views: 259
Reputation: 56557
It is kind of trivial. You define listData
in each iteration. Move it outside the loop:
var listData = [];
for (var i=0; i<count; i++) {
...
listData[i] = title;
}
Upvotes: 4