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]]}
share|improve this question

2 Answers

up vote 24 down vote accepted
dependentProperties.push({name: depName, value: depValue});
share|improve this answer
The syntax is staring you right in the face! – John Kugelman Jul 1 '09 at 3:13
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.

share|improve this answer
thanks for the nice explanation .... – afgallo Jul 1 '09 at 3:54

Your Answer

 
or
required, but never shown
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.