I’m trying to set a variable name in an unshift() call like so:
var new_index_name = priv.indices[i]["name"], // e.g. "container"
new_index_array.unshift({new_index_name :[]});
But my new_index_array will now contain:
[ {new_index_name : []} ]
instead of
[ {"container" : []} ]
Question:
Is it possible to pass a variable name as the key value in a push/unshift call?
Thanks for help!
This has nothing to do with
pushorunshiftYou can’t use a variable as a property in an object literal (since you can use identifiers for property names, and variables are also represented by identifiers).
You have to construct the object, then add the data to it.