Convert ndarray to array of arrays

Viewed 116

I'm using ndarray. I'm doing console log:

console.log(ndarray.data)

it's:

Uint8Array [ 0, 0, 0, 255 ]

how can I convert it to array of arrays (array of array in above example) like this:

[[ 0, 0, 0, 255 ]]

When I have Uint8Array like this one:

Uint8Array [
  0,   0,
  0, 255,
  0,   0,
  0, 255
]

then I would like it to be

[[ 0, 0, 0, 255 ], [ 0, 0, 0, 255 ]]

but with given answer it's

[ [
    0,   0,
    0, 255,
    0,   0,
    0, 255
  ] ]
2 Answers

Try

let u = new Uint8Array([ 0, 0, 0, 255, 0, 0, 0, 255 ]);
let a=[];

for(let i=0; i<u.length/4; i++) a.push([u[4*i],u[4*i+1],u[4*i+2],u[4*i+3]]);

console.log( a );

You can also try this way

let ua = new Uint8Array([ 0, 0, 0, 255 ]);

let aa = [[...ua]];

console.log( aa );

Related