views:

1487

answers:

2

My aim is to get a json array like this one:

var args = [{ name: 'test', value: 1 }, { key: 'test2', value: 2}];

How can I get the below code to build up an array like the above one?

this.dependentProperties = []; //array
function addDependentProperty(depName, depValue) {    
    dependentProperties.push(new Array(depName, depValue));
}

By using the push method I end up having a json notation like this one:

args:{[["test1",1],["test2",2]]}
+4  A: 
dependentProperties.push({name: depName, value: depValue});
Luca Matteis
The syntax is staring you right in the face!
John Kugelman
+3  A: 
var args = [{ name: 'test', value: 1 }, { key: 'test2', value: 2}];

...this is an array where each element is a associated-array (=hash, =object).

dependentProperties.push(new Array(depName, depValue));

...you are pushing a (sub-)Array into the parent array. That's not the same as an associative array. You now have a heterogeneous array.

dependentProperties.push({name: depName, value: depValue});

...This is pushing an associated-array into your top-level array. This is what you want. Luca is correct.

the.jxc
thanks for the nice explanation ....
afgallo