简体   繁体   English

将数组推送到数组的集合

[英]Push array to a collection of arrays

I'm trying to push an array to a collection of arrays but I'm getting unexpected results. 我正在尝试将一个数组推送到数组的集合中,但结果却出乎意料。

var itemX = ['a','b','c'];
var itemY = ['x', 'y', 'z'];
var itemList = [];

itemList.push(itemX);
itemList.push(itemY);

I'm expecting to get this: 我期望得到这个:

itemList = [
       ['a', 'b', 'c'],
       ['x', 'y', 'z']
]

But instead I'm getting this: 但是我得到了这个:

itemList = ['a', 'b', 'c', 'x', 'y', 'z']

Am I missing anything here? 我在这里想念什么吗? Any help will be appreciated. 任何帮助将不胜感激。 Thank you so much. 非常感谢。

I think it is working fine, probable reason is you might be trying to alert itemList; 我认为它工作正常,可能的原因是您可能正在尝试alert itemList; which alerts a,b,c,x,y,z . 提示a,b,c,x,y,z

 var itemX = ['a','b','c']; var itemY = ['x', 'y', 'z']; var itemList = []; itemList.push(itemX); itemList.push(itemY); alert(JSON.stringify(itemList)); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM