I need to map through an array so i can get all the notes pushed in an array params.createdNotes can i map inside the variable sortNotes so i can have a url like below :
/api/35678&page=1¬es[]=2¬es[]=4¬es[]=5
params.createdNotes = [2, 4, 5]
instance.fetchNotes = (params) => {
let sortNotes = (params.createdNotes.length === 0) ? '' : '¬es[]='+params.createdNotes;
return instance.get(
'/api/'+params.section+'&page='+params.currentPage+sortNotes
)
}
2
Answers
Use the built-in URLSearchParams API to take care of creating a valid querystring first. Then when we’re done, call
toString
to get the resulting querystring.In the context of your code:
It looks like you’re trying to append a number of query strings for an unknown number of notes whose ids are stored in the array params.createdNotes, but you’re unsure how to map through them while adding the query string. You could try pulling those URL ids with a for…in loop and using template strings: