2010-03-29 90 views
3

我以下JS功能。Javascript功能推送问题

responseData:function(resp){ 
    this.jsondata = eval('(' + resp + ')'); 
    this.propList = []; 
    for (var i = 0;i<this.jsondata.length;i++) { 
     for (obj in this.jsondata[i]) { 
      alert(obj); //shows the property name of obj 
      this.propList.push({ 
       obj : this.jsondata[i][obj] //insert only simple obj string 
      }); 
     } 
    } 
    return this.propList; 
} 

我想插入我的propList属性名称和值,但插入属性名称此函数插入​​简单的'obj'作为字符串。我做错了什么?

问候 斯特凡

回答

4

更改环路,

for (obj in this.jsondata[i]) { 
     alert(obj); //shows the property name of obj 
     var item = {}; 
     item[obj] = this.jsondata[i][obj]; 
     this.propList.push(item); 
    } 

当您使用对象的字面创建属性名称不被评估为变量的对象。要使用变量当前值指定对象属性的名称,必须使用obj[variable]格式。这将在obj内创建一个属性,其名称将与当前值variable相同。

+0

确定正常工作 ty – Mark 2010-03-29 11:24:48