简体   繁体   中英

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; which alerts 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)); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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