Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How I can exclude a member of array from Url? #327

Open
fpichlou opened this issue Sep 1, 2021 · 1 comment
Open

How I can exclude a member of array from Url? #327

fpichlou opened this issue Sep 1, 2021 · 1 comment

Comments

@fpichlou
Copy link

fpichlou commented Sep 1, 2021

I want to exclude a value of an array from query string.
How I can do this?

  const removeFilter = (filterKey, filterValue) => {
    const test = queryString.exclude(
      location.search,
      (key, value) => {
        return (
          key === filterKey &&
          (Array.isArray(value) ? value.includes(filterValue) : value === filterValue.toString())
        );
      },
      {
        arrayFormat: 'comma',
      }
    );
    history.push({ search: test });
  };
@byeze
Copy link

byeze commented May 17, 2022

I think this is what you are looking for

import querystring from 'query-string';

// Removes a value from an array given its key
const removeFilter = (filterKey, array) => {
  const filteredArray = array.filter(
    (filter) => filter.key !== filterKey
  );

  const qs = querystring.stringify(filteredArray);

  return qs;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants