actionscript 3 - AS3: Merge multiple 2d Arrays - indexOf not working -
actionscript 3 - AS3: Merge multiple 2d Arrays - indexOf not working -
i want merge multiple 2dim arrays of type [unique number][a prepare number] since merge them (and have unique results) i'm searching within "retarr" if array[unique number] present.
array1: retarr array2: arg (multiple 2dim arrays) // before here additional "for each"-loop gives me in every iteration "new" arg-array. (var p:uint = 0; p<arg.length; p++){ if(retarr.length ==0){ var tmp:array = new array(); tmp.push(arg[p][0]); tmp.push(arg[p][1]); retarr.push(tmp); } else{ for(var i:uint = 0; i<retarr.length; i++){ if (retarr[i].indexof(arg[p][0]) == -1){ var tmp:array = new array(); tmp.push(arg[p][0]); tmp.push(arg[p][1]); retarr.push(tmp); break; } } } }
i think line
if (retarr[i].indexof(arg[p][0]) == -1)
is problem, since i'm getting double-results in retarr. can help me out please?
to summarize: have array of arrays , each array in parent array has 2 elements. want filter out arrays have duplicated values @ index 0. correct?
if problem in sec loop. have in arg:
var arg:array = [[0, 1], [2, 3], [2, 3]]
on first iteration, retarr contains nothing, [0, 1] added , sec loop doesn't run
on sec iteration, sec loop executes 1 time because first element of [2, 3] not appear in [0, 1]
now here's problem: on 3rd iteration, sec loop executes. checks see if [0, 1] contains 2. doesn't, adds retarr. never checks see if [2, 3] contains 2.
what need loop through elements of retarr, , if there no matches @ all, can safely add together array.
var matchfound:boolean = false; for(var i:uint = 0; i<retarr.length; i++){ if (retarr[i].indexof(arg[p][0]) != -1){ matchfound = true; break; } } if( ! matchfound) { var tmp:array = new array(); tmp.push(arg[p][0]); tmp.push(arg[p][1]); retarr.push(tmp); }
there problem: filtering out array if it's element 0 matches array's element 1. filter out array's have duplicate element 0's, alter this
if (retarr[i].indexof(arg[p][0]) != -1){
to this
if (retarr[i].indexof(arg[p][0]) == 0){
it faster , less complicated if did this:
if(retarr[i][0] == arg[p][0]){
actionscript-3 multidimensional-array merge indexof
Comments
Post a Comment