1

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.

1
  • 2
    You are resetting listData = []; on every iteration. Commented Dec 14, 2014 at 21:52

1 Answer 1

4

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;
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.