Javascript jquery每个json对象都不做任何事情

Javascript jquery每个json对象都不做任何事情,javascript,jquery,arrays,json,Javascript,Jquery,Arrays,Json,我有一个数组和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].na

我有一个数组和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(名称)

然后我尝试从数组中获取“项”和“管道”

console.log(names.length); // give 0
$.each(names, function (key) {
        console.log('123');
        console.log(key.item + ' - ' + key.pipeline);
    })

但是我什么也没有得到,就好像函数不存在一样…

您需要将key与函数中的实际数组一起使用

    $.each(names, function (key) {
        console.log('123');
        console.log(names[key].item + ' - ' + names[key].pipeline);
    })
应该有用

或者,您需要为值添加一个变量:

    $.each(names, function (key, value) {
        console.log('123');
        console.log(value.item + ' - ' + value.pipeline);
    })

我认为
msg
中的
success:function(msgs)
不是ab对象,而是数组

管道
也不是数组,而是字符串。所以您不能使用
管道[i]

你可以用

for (var i in msg){
    console.log( msg[i].pipelines );
}
或者我建议你使用像

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

    } );
} );

console中是否有任何错误或123之后的内容未定义或任何内容?您是否可以发布
JSON
,而不仅仅是图像?我想知道您是否正在尝试在请求执行之前处理
名称。console中没有“123”。在
console.log(名称)
console.log之间一定发生了一些事情(names.length)
。可能还有其他代码在其间执行操作。请同时考虑以下内容:您的控制台显示名称列表,但顶部显示的是
数组[0]
。正如我所经历的,一些浏览器在调试器中显示处于最终状态的对象,即使您在开始时对其进行console.log。可能是当对象到达
每个
位时,名称为空,然后再填充。在控制台上,您可能看到处于最终状态的名称,而不是console.log时的名称。
names、 length=0所以它不会迭代是的,错过了那个位。可能是某种异步问题。错过了
$的参数。each()