Currently I have a unidimensional array, e.g. ['thing1', 'cond1', 'thing2', 'cond2', 'thing3']
I would like to pair each item to create a new multidimensional array like so [['thing1', 'cond1'], ['thing2', 'cond2'], ['thing3']]
. I don't mind the last item being ['thing3', undefined]
– if anything this is preferable unless someone raises this as bad practice.
So far I have
const pair = (arr) => {
let paired = [];
for (i = 0; i < arr.length; i += 2) {
paired.push([arr[i], arr[i+1]]);
}
return paired;
}
You can try this out in my JS Bin example.
This works perfectly fine AFAIA but I'd love this to be as concise as possible using modern JS and I'm not as polished as I should be with my array manipulation.
Thanks in advance to everyone who gives this a go.
Let the challenge... BEGIN!