I want to do this:
["a","b","c","d"] -> ["ab","bc","cd"]
exactly like in Join elem with next one in a functional style, but in JavaScript.
Since JavaScript doesn't have a sliding function, I'm left wondering what would be the best approach. I could do something with reduce, but it would be messy since I'm not an expert, so I'd like some advice.
You could use Array.from() to create an array with length of 1 less than that of the input array
const input = ["a", "b", "c", "d"],
length = input.length - 1,
output = Array.from({ length }, (_, i) => input[i] + input[i+1]);
console.log(output)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With