I have the below student values in an array
var arr=['sam','peter','alex']
Now I am hitting an endpoint where in I need to filter out the values based on the student names as follows.
***************/$format=json&$filter=name eq 'sam' or name eq 'peter' or name eq 'alex'&$select=id,address
getData is a custom function to connect to db and performs odata call Below is the implementation I have used.
getData([
`/student_details`,
`?$format=json`,
`&$filter=name eq '${arr}'`,
`&$select=id,address`
])
the length of the names array is not constant and it keeps varying. The above piece of code doesn't work and instead the url is framed as
***************/$format=json&$filter=name eq 'sam,peter,alex'&$select=id,address
How do I correct this?
Just use declare a variable query that later on you will use in your getData function:
var arr=['sam','peter','alex'];
var query = arr.map(e => `name eq "${e}" `).join('or ');
console.log(query);
getData([
`/student_details`,
`?$format=json`,
`&$filter='${query}'`,
`&$select=id,address`
]);
`