sindresorhus / query-string

Parse and stringify URL query strings
MIT License
6.72k stars 448 forks source link

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

Open fpichlou opened 3 years ago

fpichlou commented 3 years ago

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 commented 2 years ago

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;
}