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

Alex
  • 369
  • 2
  • 17

1 Answers1

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); } });
Anuga
  • 2,619
  • 1
  • 18
  • 27
  • Note that the `[]` arn't really necessary in the url and in the if statement, for it work. – Anuga Feb 10 '22 at 15:37
  • 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? – Alex Feb 11 '22 at 08:33
  • You should have been able to figure that one out from the code, but sure, I've added a example with no `int` restriction. – Anuga Feb 11 '22 at 13:11