Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Join elem with next one in a functional style, in JavaScript

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.

like image 874
JoseHdez_2 Avatar asked Dec 06 '25 14:12

JoseHdez_2


1 Answers

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)
like image 174
adiga Avatar answered Dec 09 '25 02:12

adiga



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!