1

I have the following URL queryString index.html?cars=honda+nissan&price=90+60+70 and need to remove all the characters =, +, &. When I chain the split it returns split is not a function

Desired output;

honda
nissan
90
60
70

JS:

const urlSearch = window.location.search;
const param = urlSearch.split('=')[1].split('&');
1

2 Answers 2

1

You could use the URL API to iterate over the searchParams instead.

const str = 'http://index.html?cars=honda+nissan&price=90+60+70';

const params = new URL(str).searchParams;

for (const [key, value] of params.entries()) {
  value.split(' ').forEach(el => console.log(el));
}

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

Comments

1

You should define what part of query you want to use and also you should use the queries this way

const urlSearch = new URLSearchParams(window.location.search);
const param = urlSearch.get("cars").split('=')[1].split('&');

for more information about getting query string check out here https://flaviocopes.com/urlsearchparams/

in your case you want to split the strings of car in the query string but you didn't mention to get it and then use it, so the data will be undefined and when you want to call a function on an undefined value it will throw following error

(FUNCTION)* is not a function

it can be anything such as map function or anything else

Comments

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.