Javascript trouble when copying element from one array to another. Extra square brackets, extra dimensions? -


starting out with:

arraya = [    ["element0"],  ["element1"],  ["element2"]    ]; 

and

arrayb = []; 

after for-loop:

arrayb[i] = arraya.splice(x,1); 

then

arrayb = [    [["element0"]],  [["element1"]],  [["element2"]]    ] 

any clue why happening?

array.splice returns array of removed items. in arraya, each item array, array.splice returns array containing array. example, arraya.splice(0, 1) returns [["element0"]]. if use populate arrayb this, you'll end array in each element array containing single array, have.

if use array.splice single element , want element returned, write arraya.splice(0, 1)[0] first element.

also, want arraya array of arrays? or want array of strings? if so, arraya = ["element0", "element1", "element2"]; , result of arraya.splice(0, 1) "element0".


Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -