jquery push 生成多维数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17075082/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jquery push to make multidimensional array
提问by Joe
I've looked at other similar posts with no help, they all start with a multidimensional array already made, I want to magically make one by using .push.
我在没有帮助的情况下查看了其他类似的帖子,它们都从一个已经创建的多维数组开始,我想通过使用 .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 'ItemsArray.Item' is not defined... which is clearly ridiculous ?
我尝试使用:ItemsArray.Item.push{ stuff:morestuff }
但它停止说'ItemsArray.Item' is not defined...这显然是荒谬的?
Also tried: ItemsArray[1].push{}
with same error...
也试过:ItemsArray[1].push{}
有同样的错误...
Surely this must be a stupid simple problem.
当然,这一定是一个愚蠢的简单问题。
Thanks!
谢谢!
回答by DSlagle
You are creating Item as an object. You want it to be an array to be able to push into it.
您正在创建 Item 作为对象。您希望它是一个能够推入其中的数组。
var ItemArray = [];
ItemArray.push({
RoomName : 'RoomName',
Item : []
});
ItemArray[0].Item.push("New Item");
console.log(ItemArray);
Hereis a good blog post that has in-depth detail about the difference between objects and arrays.
这是一篇很好的博客文章,其中详细介绍了对象和数组之间的区别。
回答by Uday Hiwarale
var tdarray = [[]];
tdarray[0].push(22);
tdarray[0].push(23);
alert(tdarray[0][1]); //you can change this