Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get data from each array of array with sequence output

i am trying to get sequence index of each array of array like I have an element of the array and that 5 arrays have multiple elements and I want to get the first index of each array then the second and so.

below is my array data

const arrData = [[1,2,4,6],[3,8,7],[12,13],[],[9,10]];

and my expected outout [1,3,12,9,2,8,13,10,4,7,6]

give me some sort of solution to fix my issue

like image 851
Trushar Narodia Avatar asked Dec 02 '25 09:12

Trushar Narodia


1 Answers

Following might work:

const src = [[1,2,4,6],[3,8,7],[12,13],[],[9,10]],

      pickByOne = arr => {
        const result = [],
              {length} = arr.flat()
        while(result.length < length){
          for(a of arr){
            a.length && result.push(a.shift())
          }
        }
        return result
      }
      
console.log(pickByOne(src))
like image 107
Yevgen Gorbunkov Avatar answered Dec 04 '25 01:12

Yevgen Gorbunkov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!