gpt4 book ai didi

javascript - 循环数组并创建查询

转载 作者:行者123 更新时间:2023-12-01 02:02:58 24 4
gpt4 key购买 nike

我有一个数组中的值列表,我需要基于该列表创建一个查询

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

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

ID 的数量根据数组中的值而增加。

我尝试创建一个 for 循环,然后添加 i 例如:

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 block ,因为“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 的空格编码。

关于javascript - 循环数组并创建查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50400865/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com