Reputation: 5558
I am trying to create the following nested object inside a for loop using JavaScript which then gets pushed to an existing array:
_spec = {
_key: {
type: _clHndl.getFieldType(_f),
editable: true,
validation: {
required: _clHndl.isRequired(_f),
min: 10
}
}
};
_arr.push(_spec);
The _key field is dynamic (changes every iteration), I want the identifier of the nested item _key to be the actual value that _key contains in the iteration. Right now it just makes each one '_key' when I use JSON.stringify() to inspect it.
Any help would be appreciated. Thanks for your time.
Upvotes: 0
Views: 996
Reputation:
Your key isn't really nested (it's at the top level of the outer object), though it wouldn't really be different if it was.
To create a dynamic key, use the square bracket version of the member operator.
spec = {};
spec[my_dynamic_key] = {
type: _clHndl.getFieldType(_f),
editable: true,
validation: {
required: _clHndl.isRequired(_f),
min: 10
}
}
Upvotes: 1