Skip to content
Advertisement

Combine neighbouring elemets to new array of min 2

I have a quite specific problem. I want to combine every element of an array with neighboring elements to a new array of minimum length 2. Im not sure if Im explaining that correctly so here’s an example:

Given the array:

JavaScript

I want to create a new array that looks like this:

JavaScript

[‘abcd’] is not returned since it is only of length 1.

Advertisement

Answer

I thought I would post an answer since I got it working with help from others.

JavaScript
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement