简体   繁体   中英

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

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}`:""
})

try this

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

在此处输入图像描述

You can 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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM