I have arrays like:
a = ['a', 'b', 'c']
b = [1, 2, 3]
and I would like to get:
[['a', 1], ['b', 2], ['c', 3]]
Is there some elegant (functional) way to do this, or will I have to use loops and indexes?
I have arrays like:
a = ['a', 'b', 'c']
b = [1, 2, 3]
and I would like to get:
[['a', 1], ['b', 2], ['c', 3]]
Is there some elegant (functional) way to do this, or will I have to use loops and indexes?
Would you consider something like this "elegant"?
a.map((x, idx) => [x, b[idx]]);
Option 1
var a = ['a', 'b', 'c']
b = [1, 2, 3];
var zip = [];
for (var i = 0; i < a.length; i++){
zip.push([a[i], b[i]]);
}
OR to use fewer lines i.e. more 'elegant' approach:
Option 2
var a = ['a', 'b', 'c']
b = [1, 2, 3];
var zip = (b, a) => b.map((k, i) => [k, a[i]]);
// Instead of the for loop
// [['a', 1], ['b', 2], ['c', 3]]