0
1

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]]}
flag

2 Answers

3
dependentProperties.push({name: depName, value: depValue});
link|flag
The syntax is staring you right in the face! – John Kugelman Jul 1 '09 at 3:13
2
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.

link|flag
thanks for the nice explanation .... – afgallo Jul 1 '09 at 3:54

Your Answer

get an OpenID
or
never shown

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