Javascript对象返回垃圾值 [英] Javascript Object returns junk values

查看:114
本文介绍了Javascript对象返回垃圾值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为javascript对象赋值,当这样做时,一些垃圾值最终会出现在像'push','pop','splice'等数组方法中。以下是我的代码。

I'm trying to assign values to a javascript object and when doing so, some junk values end up in there which seem like array methods like 'push', 'pop','splice' etc. The following is my code.

function myTest(){
var userArray = new Object();
var req = new Request.JSON({
url: '/myTest.php',
method: 'post',
noCache: true,
data: 'userID=999',

onSuccess: function(json){       
    for(var key in json){
      userArray = json[key];
      for (var row in userArray){
        alert(row)   // This returns values like '$family','push','pop', 'reverse' etc.
        }
    }
},
onException: function(xhr){
  alert("Unable to process your request");
},
onFailure: function(xhr){
  alert("Unable to connect to the server");
}
}).send();
}

我不确定我在这里缺少什么,但看起来我肯定上午。任何有关这方面的帮助将不胜感激。

I am not sure what I'm missing here but it looks like I certainly am. Any help on this would be greatly appreciated.

推荐答案

永远不要在数组中使用... in。期。您看到的垃圾值是数组原型的属性。

Never use for...in on an array. Period. The garbage values you are seeing are properties of the array prototype.

请参阅相关问题

这篇关于Javascript对象返回垃圾值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆