简体   繁体   English

遍历数组并创建查询

[英]Loop over array and create a Query

I have a list of values in a array, I need to create a query based on that 我在数组中有一个值列表,我需要基于该列表创建一个查询

var x = [1, 2, 3, 4, 5];

url = http://localhost:3000/site/query=("ID:"+ 1 + "ID:" + 2 + "ID:" + 3)

the number of ID increases based on values in an array. ID的数量根据数组中的值增加。

I tried creating a for loop and than add i for example: 我尝试创建一个for循环,然后添加例如:

for (var i = 0; i < x.length; i++) {
  if (i === 0) {
    url = http://localhost:3000/site/query=("ID:"+ x[i])
  }
  if (i === 1) {
    url = http://localhost:3000/site/query=("ID:"+ x[0] + "ID:" + x[i])
  }
}

I cannot create multiple if blocks because the "i"value can be dynamic and there could be many values in array 我无法创建多个if块,因为“ i”值可以是动态的,并且数组中可以有很多值

I mean, if that's really what you want, you can just join the array. 我的意思是,如果这确实是您想要的,则可以加入阵列。

 var x = [1]; var url = 'http://localhost:3000/site/query=ID:'+x.join('%20OR%20ID:') console.log(url); var x = [1, 2, 3, 4, 5]; var url = 'http://localhost:3000/site/query=ID:'+x.join('%20OR%20ID:') console.log(url); 

%20 is space encoded for urls. %20是URL的空格编码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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