【问题标题】:Window object appearing in array出现在数组中的窗口对象
【发布时间】:2012-11-26 15:28:06
【问题描述】:

当我运行下面的代码时,jQuery 反复将一个 Window 对象放入我的数组中(并且我确信 JSON 不包含对 Window 对象的任何引用)。有什么方法可以运行它并将 Window 对象排除在我的数组之外?

谢谢

$.getJSON("../php/return_network_data.php",
function(fetch_data){
    $.each(fetch_data, function(){
    var index = this.id;
    node_array[index] = paper.circle(this.xpos_init, this.ypos_init, 10).attr({"fill" : "#ff0000"})

    $.each(node_array, function(){
            console.log(this);
      });
  });
}
);   

【问题讨论】:

  • 是什么让你认为 jQuery 正在这样做,而不是 raphael 或其他代码?我会查看您的paper.circle(...).attr(...) 的文档,看看它是否可以返回window。如果没有,请查看代码中的其他位置。

标签: javascript jquery arrays window raphael


【解决方案1】:

这应该可以工作..必须在每个函数中添加索引、值

$.getJSON("../php/return_network_data.php",
function(fetch_data){
    $.each(fetch_data, function(index,value){
    var index2 = value.id;
    node_array[index] = paper.circle(this.xpos_init, this.ypos_init, 10).attr({"fill" : "#ff0000"})

    $.each(node_array, function(index,value){
            console.log(value);
      });
  });
}
);

【讨论】:

  • 不,jQuery 也通过this 为您提供value,这就是OP 似乎正在使用的。
  • 有没有办法让我的JS数组的索引是JSON对象的ID?
  • @user1114864 ..好吧,这取决于你制作 JSON 对象的方式。,..你当然可以做到......
  • @user1114864 仅当您的 id 以 0 开头并且没有任何缺失索引时。否则,它必须是一个数组,值或对象/数组。
【解决方案2】:

我不确定 paper.circle 到底是做什么的(这是 raphael 吗?),但我认为这可能是两件事之一:

  1. 将 attr 函数从 paper 命令中移除,稍后再添加。

    node_array[index] = paper.circle(this.xpos_init, this.ypos_init, 10);
    
  2. 在每个函数中添加键、值:

    $.getJSON("../php/return_network_data.php", function(fetch_data){
        $.each(fetch_data, function(key, value){
            var index = value.id;
            node_array[index] = paper.circle(value.xpos_init, value.ypos_init, 10).attr({"fill" : "#ff0000"})
    
            $.each(node_array, function(){
                console.log(this);
            });
        });
    });
    

或者两者都有?

【讨论】:

    猜你喜欢
    • 2021-08-03
    • 2014-04-08
    • 1970-01-01
    • 2022-01-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-26
    相关资源
    最近更新 更多