Today I had to create a function which added one element after every single element in an array. E.g.
['a', 'a', 'a'] => ['a', 'b', 'a', 'b', 'a', 'b']
Because of the nice array methods like map, and filter and such I searched for a similar method for injecting elements accepting an arrow function but I couldn't find one. I thought an inject method might be a nice addition following this pattern:
[1,2,3].inject(el => el > 2, el => el += 2);
The first parameter being a boolean function to see where to inject a new element, and the second parameter being or returning a value to inject in that position giving the result:
[1,2,3,5,4,6]
Like map it wouldn't mutate the original array but instead return a new one.
I'd like to hear what other people think of this idea.