Reputation: 672
I have an array and ajax function. I'm adding the objects into array.
var names = [];
...
success: function(msg){
for (var i in msg.response.pipelines){
for (var j in msg.response.pipelines[i].statuses){
names.push({
pipeline: msg.response.pipelines[i].name,
item: msg.response.pipelines[i].statuses[j].name
});
}
}
Then I see in console console.log(names)
Then I'm trying to get "item" and "pipeline" from array.
console.log(names.length); // give 0
$.each(names, function (key) {
console.log('123');
console.log(key.item + ' - ' + key.pipeline);
})
But I'm getting nothing as if the function does not exist...
Upvotes: 0
Views: 105
Reputation: 2621
I think msg
in success: function( msgs )
is not ab object but an array.
pipelines
is also not an array but a string. so you can't use pipelines[i]
you can use
for (var i in msg){
console.log( msg[i].pipelines );
}
or I suggest you to use like
success: function(msg){
$.each( msg, function( index, item ) {
console.log( item ) // this will be { "item" : "xxxx", "pipeline" : "xxxxxxxxxx"}
// Now check what you want to do
} );
} );
Upvotes: 1
Reputation: 123
You need to use key with the actual array in your function
$.each(names, function (key) {
console.log('123');
console.log(names[key].item + ' - ' + names[key].pipeline);
})
Should work.
Alternatively you need to add a variable for the value:
$.each(names, function (key, value) {
console.log('123');
console.log(value.item + ' - ' + value.pipeline);
})
Upvotes: 0