简体   繁体   中英

how to pop multidimensional array by going to each element

i wanted to pop each element one by one let say i have this array [[1,2],[3]]

i will pop first time by going to inside array element 1, then 2 (since 2 is last element i want to notify or log saying it is last element)

Question : i want to pop each array one by one if last element of each array encountered then i want to notify( console.log('last element') )

here is the code i was trying:

 var mulA = [[1,2],['a','b','c'],['s'],['g','h'],['rr','tt','mm'],[],['q']]; var intId = setInterval(function(){ var poped = mulA.pop(); console.log(poped); console.log('notify on pop of each array last element'); },1500); 

If you want to pop the array that is inside the array, try it:

 var mulA = [[1,2],['a','b','c'],['s'],['g','h'],['rr','tt','mm'],[],['q']]; var poped = mulA.pop(); var intId = setInterval(function(){ if(poped == undefined || poped.length < 1) { if(mulA.length < 1) { clearInterval(intId); console.log('finished'); return; } poped = mulA.pop(); console.log('last element'); return; } console.log(poped.pop()); console.log('notify on pop of each array last element'); },1500); 

You could chekc the lenght of the inner arrays and the outer array and print a notification or end the interval.

 var mulA = [[1,2],['a','b','c'],['s'],['g','h'],['rr','tt','mm'],[],['q']], intId = setInterval(function(){ if (!mulA.length) { clearInterval(intId); return; } console.log(mulA[0].shift()); if (!mulA[0].length) { while (mulA[0] && !mulA[0].length) mulA.shift(); console.log('notify on pop of each array last element'); } }, 500); 

First print the inner array and when its length is equal to 1 than it is last element in that array, And when the length of inner array becomes 1 only than pop from the outer array.

Try the following:

 var mulA = [[1,2],['a','b','c'],['s'],['g','h'],['rr','tt','mm'],[],['q']]; var poped = mulA.pop(); var intId = setInterval(function(){ if(poped.length){ if(poped.length == 1){ console.log(poped.pop()); console.log('notify on pop of each array last element'); if(!mulA.length){ clearInterval(intId); return; } }else{ console.log(poped.pop()); } } else { poped = mulA.pop(); } },150); 

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