sindresorhus/query-string

How I can exclude a member of array from Url?

fpichlou opened this issue · 1 comments

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

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