0

I need to pass an array of objects to a php page as part of a larger data structure. I am trying to replicate this structure in JavaScript and pass it via json.

The arrays keys in php have been set as names which I require later in the process.

I know JavaScript doesn't use associated arrays and arrays in JavaScript are essentially objects but documentation suggests I should be able to use named keys.

If this is the case I should also be able to use variables as named keys by using a different syntax.

Can someone then please show me what I am doing wrong?

Example 1: numeric keys (works)

var dataTargets = [];
var obj = {
  'test': 'test'
};
dataTargets["0"] = obj;
alert(JSON.stringify(dataTargets));

Example 2: named keys (fails)

var dataTargets = [];
var obj = {
  'test': 'test'
};
dataTargets["test"] = obj;
alert(JSON.stringify(dataTargets));
//outputs []

Example 3: variable keys (fails)

var dataTargets = [];
var dtname = "test";
var obj = {
  'test': 'test'
};
dataTargets[dtname] = obj;
alert(JSON.stringify(dataTargets));
//outputs []

2
  • Is question why JSON.stringify() returns an array with out elements set? What is expected result? Commented Mar 7, 2017 at 2:50
  • Could you try: dataTargets = {}; dataTargets['test'] = obj; Commented Mar 7, 2017 at 2:50

2 Answers 2

4

The properties are actually being correctly assigned to each array; the problem is that JSON.stringify ignores all non-index properties of arrays (treating them more like lists and less like objects). If you want to use named keys in your objects, you will have to use plain objects {} rather than arrays []:

var alert = console.log.bind(console) // for demo purposes


// Example 1: numeric keys 

var  dataTargets = {};
var obj = {'test':'test'};
dataTargets["0"] = obj;
alert(JSON.stringify(dataTargets));


// Example 2: named keys

var  dataTargets = {};
var obj = {'test':'test'};
dataTargets["test"] = obj;
alert(JSON.stringify(dataTargets));


// Example 3: variable keys 

var  dataTargets = {};
var dtname = "test";
var obj = {'test':'test'};
dataTargets[dtname] = obj;
alert(JSON.stringify(dataTargets));
//outputs []

Sign up to request clarification or add additional context in comments.

1 Comment

Thankyou for this answer, it clarified both of the things I was trying to work out (was I assigning the value correctly or was JSON.stringify not accepting it due to the key not being numeric). For reference I am going to modify the object on the php page receiving the data to have an additional property rather than using the keys. Thanks again :-)
0

what you did actually like this

example1:

var  dataTargets = [];
var obj = {'test':'test'};
dataTargets[0] = obj;
alert(JSON.stringify(dataTargets));
// outputs [{test:test}]

what you need is:

var  dataTargets = {};
var obj = {'test':'test'};
dataTargets["test"] = obj;
alert(JSON.stringify(dataTargets));
//{"test":{{test:test}}}

because array need to be access by index. And object can be what you need

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.