-1

Is there a simple way to convert this array to a multidimensional array in javascript?

[ false,'Licht robuust geschaafd, geschuurd en geborsteld.',false,'Midden robuust geschaafd, geschuurd en geborsteld.']

Like this

[false,'Licht robuust geschaafd, geschuurd en geborsteld.'],
[false,'Midden robuust geschaafd, geschuurd en geborsteld.']

Kind regards,

Stephan

amrz one
  • 123
  • 8
  • Can you please show us what you've tried so far? We can help you get it working. –  Apr 14 '21 at 14:08

1 Answers1

0

Assuming what you want is an array of array:

[
  [
    false,
    "Licht robuust geschaafd, geschuurd en geborsteld."
  ],
  [
    false,
    "Midden robuust geschaafd, geschuurd en geborsteld."
  ]
]

And assuming your array can be very large, I wouldn't create any extra data structure that takes up a lot of space, so the simple solution can be just to create an empty array and add to it:

const arr = [false, 'Licht robuust geschaafd, geschuurd en geborsteld.', false, 'Midden robuust geschaafd, geschuurd en geborsteld.'];

console.log(arr);

const results = [];
let iResult = 0;
for (let i = 0; i < arr.length; i += 2) {
  results[iResult] = [];
  results[iResult].push(arr[i]);
  results[iResult].push(arr[i + 1]);
  iResult++;
}
console.log(results);
kla
  • 63
  • 8