1

How to create multidimensional array??

I tried it so far:

var post_data = [];
var id = 1;

post_data[id] = [];
post_data[id]['name'] = 'abc';
post_data[id]['date'] = '01-03-2014';
post_data[id]['country'] = 'India';

console.log(post_data);

the above code is not giving me the key. Whats wrong?

DEMO FIDDLE

i want a output something like this:

[1]=> array
    (
       "name": "abc",
       "date": "01-03-2014",
       "country": "India",
    )

How to get the above output???

user3030089
  • 262
  • 1
  • 12
  • 3
    Javascript doesn't have multidimensional arrays rather object arrays or array objects not the way your trying. You'd either have to start with an object or start with an array and add objects. `var post_data= {};` or push arrays within the array and their names are 0 1 2 3 etc. – EasyBB Jul 28 '14 at 11:19

4 Answers4

2

To get wished result you can change

var post_data = [];

to

var post_data = {};

and

post_data[id] = {};
Gena Moroz
  • 910
  • 5
  • 7
  • The output that the OP is looking for id say leave the original line and add objects hes looking for php like output. And by doing the array first and then pushing objects he can iterate over with a regular for loop then go over his object. Depending on the OPs method hes looking for id say this isnt the best answer and should declare some more information declaring as to why and other methods for furture references and less duplicate questions. – EasyBB Jul 28 '14 at 11:30
1

You are trying to make an array of object.

Try this : -

post_data[id] = {};

Kundu
  • 3,300
  • 3
  • 13
  • 20
1

You are using the inner array as an object. The properties that you set on the array still is there, when you display it only the array items are shown.

You should use an object instead of an array, as you are not using the array as an array:

post_data[id] = {};

Instead of setting the properties after creating the object, you can set them when you create it:

post_data[id] = {
  name: 'abc',
  date: '01-03-2014',
  country: 'India'
};
Guffa
  • 640,220
  • 96
  • 678
  • 956
0

In the third line of code you have to write

post_data[id] = new Array();

So the entire code section looks like

var post_data = [];
var id = 1;

post_data[id] =  new Array();
post_data[id]['name'] = 'abc';
post_data[id]['date'] = '01-03-2014';
post_data[id]['country'] = 'India';

console.log(post_data[id]['name']);

This should fix it, best of luck :)

Siddhant Swami
  • 305
  • 3
  • 13
  • 1
    Multidimensional arrays in JavaScript is a bad idea. As is the `new Array()`. – pattmorter Jul 28 '14 at 11:23
  • Using `new Array()` and `[]` has the same effect, so this changes nothing. – Guffa Jul 28 '14 at 11:23
  • Yeah new Array is slower than `[]` javascript is a funny language with itself really. Since arrays are objects they should be written sort of like php does their php4 `array()` but either way thats not the point. – EasyBB Jul 28 '14 at 11:26
  • 1
    They DO NOT have the same effect. `new Array()` predefined a length, `[]` does not. When you do `[]`, you are creating an array. When you do `new Array()` you are creating an **object with the functionality of an array**. http://stackoverflow.com/a/1273936/2068709 – pattmorter Jul 28 '14 at 11:26
  • @pattmorter: Sorry, you are mistaken. The result of `new Array()` and `[]` are completely identical. Both are objects of the type `Array`. Both have the length zero, as that is what the array constructor does when you don't specify a length, and because an empty array literal contains zero items. – Guffa Jul 28 '14 at 11:29
  • They are not identical. Go and create two arrays. One with `[]` and the other with `new Array()` and then try and push data into each "array". In no language can you tell me that an array is identical to an object. No where. They may *look* identical but they are everything but. – pattmorter Jul 28 '14 at 11:30
  • 1
    Lets just speak on speed variants the literal is faster than the the new object instantiated method. – EasyBB Jul 28 '14 at 11:31
  • @pattmorter: Just to please you, I have gone and created two arrays and pushed data into them. As expected, the result is identical. http://jsfiddle.net/GZ3p6/ – Guffa Jul 28 '14 at 11:34