Reputation: 33
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.
I tried creating a for loop and than add i for example:
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
Upvotes: 0
Views: 64
Reputation: 24965
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.
Upvotes: 2