views:

57

answers:

1

Hello 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

+3  A: 

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.

Chandru
ok works finety
Mark