Split JSON into two arrays gives undefined keys-values

๐Ÿ‘:-1

You are using a for in loop, instead of a for loop.

for(var i in data) {
            applications.push("App name " + data[i]['name']);
            number.push(data[i].number);
        }

should be

for(var i = 0; i < data.length; i++) {
            applications.push("App name " + data[i]['name']);
            number.push(data[i].number);
        }

๐Ÿ‘:-1

You are getting an array of objects. With jQuery, you can approach the 2 arrays like this:

var data = [{"name":"Messenger","number":"29"},{"name":"Viber","number":"28"},{"name":"Facebook","number":"28"},{"name":"Skype","number":"17"}];

var names = [];
var numbers = [];

$.each(data, function(i, row) {
    names.push(row.name);
    numbers.push(row.number);
});

Fiddle

Leave a comment