簡體   English   中英

遍歷數組並創建查詢

[英]Loop over array and create a Query

我在數組中有一個值列表,我需要基於該列表創建一個查詢

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

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

ID的數量根據數組中的值增加。

我嘗試創建一個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])
  }
}

我無法創建多個if塊,因為“ i”值可以是動態的,並且數組中可以有很多值

我的意思是,如果這確實是您想要的,則可以加入陣列。

 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是URL的空格編碼。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM