I am having trouble passing an array of objects to a php page within a larger data structure. My JavaScript code is attempting to replicate this structure and pass it via json.
In the php script, the array keys have been set as names that I will need later in the process.
Although JavaScript does not use associated arrays, arrays can be treated as objects. The documentation suggests that named keys can still be used.
It seems that I should also be able to use variables as named keys with a different syntax. Can someone help me identify what mistake I may be making?
Example 1: Using numeric keys (works)
var dataTargets = [];
var obj = {
'test': 'test'
};
dataTargets["0"] = obj;
alert(JSON.stringify(dataTargets));
Example 2: Using named keys (fails)
var dataTargets = [];
var obj = {
'test': 'test'
};
dataTargets["test"] = obj;
alert(JSON.stringify(dataTargets));
//outputs []
Example 3: Using variable keys (fails)
var dataTargets = [];
var dtname = "test";
var obj = {
'test': 'test'
};
dataTargets[dtname] = obj;
alert(JSON.stringify(dataTargets));
//outputs []