我有一个数组和ajax函数。我正在将对象添加到数组中。
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
});
}
}
然后我在控制台中看到 console.log(names)
然后我尝试从数组中获取“item”和“pipeline”。
console.log(names.length); // give 0
$.each(names, function (key) {
console.log('123');
console.log(key.item + ' - ' + key.pipeline);
})
但是我什么也没得到,就好像该函数不存在一样......
最佳答案
我认为 success: function( msgs )
中的 msg
不是 ab 对象,而是一个数组。
pipelines
也不是数组而是字符串。所以你不能使用pipelines[i]
你可以使用
for (var i in msg){
console.log( msg[i].pipelines );
}
或者我建议你使用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
} );
} );
关于javascript - jquery 每个 json 对象不执行任何操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41893641/