0

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

5
  • So the desired pattern is ID:ID:ID:ID: without a separator character? Commented May 17, 2018 at 21:40
  • I need to build a query, for example if I have two values in array [1,2], the url will be localhost:3000/site/query=("ID:"+ 1 + "ID:" + 2) Commented May 17, 2018 at 21:42
  • So the result would be ID:1ID:2 ? Commented May 17, 2018 at 21:42
  • yes, if there is only one number, only one ID will be there Commented May 17, 2018 at 21:43
  • thanks for taking time and looking into this talpar Commented May 17, 2018 at 21:44

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

answer should be ID:1 OR ID:2 OR ID:3 OR ID:4 OR ID:5

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.