开发者

Javascript function push problem

i've following JS function.

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;
}

I want to insert in my propList the 开发者_运维知识库property name and the value, but instead inserting the property name this function inserts simple 'obj' as a string. What i'm doing wrong?

greetings Stefan


Change the loop to,

    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);
    }

When you use object-literal to create an object the property names are not evaluated as variables. To specify the name of an objects property using a variables current value, you must use the obj[variable] format. This will create a property within obj whose name will be the same as current value of variable.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜