0
const [filters,filtersSetter]=useState({});

function setNewSearchQuery(param,value) {

 filtersSetter(preval=>({...preval,[param]:value}))
}

In my useState, if i want to keep everything in preval except for one key in my JSON object what do i do? i want to remove the key and its value basically, all in one line as opposed to breaking down the object first then applying changes. That is how we update the current state, is there a way to remove one from it? example

filters={ username="sample" , role="roleSample"}

when given username i want to be left with

    filters={ role="roleSample"}
ariannnn
  • 47
  • 1
  • 2
  • 8
  • Just make a copy of the object with spread operator (while remembering spread only does shallow copy) then use the `delete` keyword to remove the desired property. If you're already using a library like lodash in your project it has an `omit` function that'll do this for you – Jayce444 Apr 28 '21 at 01:00
  • What is the key you're trying to remove? Please provide a [mcve]. – Emile Bergeron Apr 28 '21 at 01:02
  • My quick guess: `filtersSetter(({ removedKey, ...rest }) =>({ ...rest, [param]: value }))` – Emile Bergeron Apr 28 '21 at 01:03
  • @EmileBergeron just added an example – ariannnn Apr 28 '21 at 01:07
  • Does this answer your question? [Filter object properties by key in ES6](https://stackoverflow.com/questions/38750705/filter-object-properties-by-key-in-es6) – Emile Bergeron Apr 28 '21 at 01:12

0 Answers0