0
 if(window.location.href.includes('part_category_tags[]=2')) { this.checkedCategories.push(2); } 

Needed: depending on what the number is next to the part_category_tags[]= parameter, push this number into the array. also, it may be that such a parameter occurs twice like (Or not be at all):

&part_category_tags[]=3&part_category_tags[]=2 

Without vue-router

2

1 Answer 1

1

You can just loop through the params.

const url = new URL('https://fake.domain.name/?fake=query&part_category_tags[]=3&part_category_tags[]=2'); // document.location instead of fake utl. let params = url.searchParams; params.forEach((v,q) => console.info(q, v)); // just output queries and values params.forEach((v,q) => { if (q === 'part_category_tags[]' && v === '2') { console.info('Pushing the 2') } }); // params.forEach((v,q) => { if (q === 'part_category_tags[]' && v === '2') { this.checkedCategories.push(v); } });

Updated with numberless code.

// // use document.location instead of fake url. const url = new URL('https://fake.domain.name/?fake=query&part_category_tags=3&part_category_tags=2'); let params = url.searchParams; params.forEach((v,q) => { if (q === 'part_category_tags') { console.info(`Pushing the ${v}`) } }); // params.forEach((v,q) => { if (q === 'part_category_tags') { this.checkedCategories.push(v); } });

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

3 Comments

Note that the [] arn't really necessary in the url and in the if statement, for it work.
As I said, I don't know what the number will be after part_category_tags, I just need to push the number that will be. Is it possible to improve your code in this direction?
You should have been able to figure that one out from the code, but sure, I've added a example with no int restriction.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.