Jquery push to create a multidimensional array

I looked at other similar messages without help, they all start with an already made multidimensional array, I want to magically create it using .push.

My array:

ItemsArray.push({ RoomName : RoomName, Item : {//this is where I want the multi-array } }); 

I tried using: ItemsArray.Item.push{ stuff:morestuff } , but it stopped saying that “ItemsArray.Item” is not defined ... which is clearly funny?

Also tried: ItemsArray[1].push{} with the same error ...

Of course, this should be a silly simple problem.

Thanks!

+6
source share
2 answers

You create an object as an object. You want it to be an array that could be inserted into it.

 var ItemArray = []; ItemArray.push({ RoomName : 'RoomName', Item : [] }); ItemArray[0].Item.push("New Item"); console.log(ItemArray); 

Here is a good blog post that details the differences between objects and arrays.

+20
source
 var tdarray = [[]]; tdarray[0].push(22); tdarray[0].push(23); alert(tdarray[0][1]); //you can change this 
+3
source

Source: https://habr.com/ru/post/947160/


All Articles