Add new string between every current string in array

Viewed 61

I have an array of topics

["dogs", "cats", "fish", "morestuff"]

I'm formatting this array afterwards to be used as a series of identifies to match within match(). Considering this list will grow I'm essentially using this array of strings to convert into a large conditional statement of OR operators. Which will be executed within eval(x.join('')). So i'd like a way to generate || operators between every existing string in the array.

This is the desired output

["dogs", "||", "cats", "||", "fish", "||", "morestuff"]

Free feel to share a better method if this is convoluted. Thank you

1 Answers

var array = ["dogs", "cats", "fish", "morestuff"],
    result = array.reduce((r, a) => r.concat(a, "||"), []);
    
console.log(result);

Related