i'm experiencing odd behavior arrays. don't understand what's happening. if create 2 arrays...
var = [0,0,1,0]; var b = [0,2, 1,2]; and combine them...
combinechars(); function combinechars(){ combo.push(a); combo.push(b); } i end array...
combo = [0,0,1,0,0,2,1,2]; however, if attempt split array 2 separate arrays doesn't split expected.
function splitarray(a){ console.log("splitarray("+a+")"); //splitarray(0,0,1,0,0,2,1,2) (i=0;i<a.length;i++){ if ((i+2)%2==0) { rsplit.push(a[i]); } else { csplit.push(a[i]); } } console.log("rsplit = " + rsplit); //rsplit = 0,0,1,0 console.log("csplit = " + csplit); //csplit = 0,2,1,2 } expected...
console.log("rsplit = " + rsplit); //rsplit = 0,0,1,0,0,1,0,1 console.log("csplit = " + csplit); //csplit = 0,2,1,2,0,0,2,2 fiddle: https://jsfiddle.net/taylorrichie/yrzrqm38/4/
as can see fiddle, if create array "co" instead of combining 2 arrays, behaves expected.
???
any appreciated.
richie
given empty combo array, , a , b arrays,
combo.push(a); combo.push(b); does not push elements of a , b combo, claim, instead, pushes arrays themselves, making combo array 2 elements, both elements being original a , b arrays themselves.
if use concat instead of push, combo array constructed expected.
i.e. change combinechars function following:
function combinechars(){ combo = combo.concat(a); combo = combo.concat(b); } note need reassign combo, because concat not mutate original array, returns new one.
Comments
Post a Comment