3

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

srb633
  • 793
  • 3
  • 8
  • 16
  • 1
    Does this answer your question? [How to insert a new element in between all elements of a JS array?](https://stackoverflow.com/questions/46528616/how-to-insert-a-new-element-in-between-all-elements-of-a-js-array) – kmoser May 16 '20 at 04:33
  • 1
    You may do "x.join('||')". I'm not sure eval is the right choice though. Why not just x.some((v) => !!v); – vitkarpov May 16 '20 at 04:33
  • Oh why didn't I think of that lol. I'll give it a try – srb633 May 16 '20 at 04:35
  • https://regex101.com/r/N53eju/4 response to your deleted question (finding the @@ and doing replace, in JavaScript). Let me know if it works. – Gustav Rasmussen May 30 '20 at 18:22

1 Answers1

4

var array = ["dogs", "cats", "fish", "morestuff"],
    result = array.reduce((r, a) => r.concat(a, "||"), []);
    
console.log(result);
Avinash Dalvi
  • 8,551
  • 7
  • 27
  • 53