Is there a way to create a JSON object ID with a variable?
var json_result = [];
var id = 20;
json_result.push({id: {parentId: parentId, position: position}});
This results into a json object with the value ‘id’ as the id. I want to achieve to have the value ’20’ as the key.
EDIT: Include solution:
var json_result = {};
var id = 20;
json_result[id] = {parentId: parentId, position: position};
Now you can access parentId and position like this:
json_result[20].position
json_result[20].parentId
You cannot write such an object literal (it’s not a “JSON object” by the way; just a plain Javascript object), but you can do it like this: