Home > Net >  How do I map an array and get the result as following by appending the previous and next value?
How do I map an array and get the result as following by appending the previous and next value?

Time:01-30

lets say I have an array of ID

id = [10,12,13,14,15]

I wish to map the id and get the following output as follow:

"id=10 OR id=12 OR id=13 or id=14 or id=15"

I tried the following but did not get the result as expected

let tempArray=  id.map((item, index)=>{
  return index  ? `id=${item} OR id=${item}`:""
})

CodePudding user response:

try this

id.map(item => `id=${item}`).join(' OR ')

enter image description here

CodePudding user response:

You cal map each value to the string id=${value} and then join the result with " OR ":

const ids = [10,12,13,14,15];
const str = ids.map((value) => `id=${value}`).join(" OR ");
console.log(str);

  • Related