DEV Community

Discussion on: What if we had a method on arrays called `.combineWith`?

Collapse
 
gillemic profile image
Michael Gillett

I was able to achieve the same result using the reduce method.

const some_tuples = [
[10, "Awesome"],
[4, "Okay-ish"],
[2, "Oh hell no"]
];

function combine_reducer(tuple_a, tuple_b) {
return [
tuple_a[0] + tuple_b[0],
tuple_a[1].concat(', ', tuple_b[1])
]
}

console.log(some_tuples.reduce(combine_reducer));

running this through node gave me:
$ node combine_with.js
[ 16, 'Awesome, Okay-ish, Oh hell no' ]