3

Hey I'm trying to remove a key:value pair from state inside a Javascript Object.

It works when I hardcode the key name in the code, but when I try to use a variable from a function call, it does nothing.

Can somebody help me out?

Here's an object example:

  toppingsSelected: {
     "Onion":"true",
     "Mushrooms":"true",
  }

This works, hardcoded:

deleteTopping = toppingName => {

   const { Onion, ...withoutOnion } = toppingsSelected;
   console.log(withoutOnion); // Returns object without onion

  };

This doesn't work:

deleteTopping = toppingName => {


   const toppingName = "Onion"; // Variable gets passed in

   const { toppingName, ...withoutOnion } = toppingsSelected;
   console.log(withoutOnion); // Returns original object, no change made

  };

So I'm basically trying to remove a key from React state but I'm pretty new to Javascript.

How can I make Javascript aware that toppingName is a key?

1
  • As far as I know you can't use dynamic keys while destructing. So, go for alternative provided answer. Commented Aug 2, 2018 at 13:19

3 Answers 3

6

Another option is to add square brackets arround toppingName, and assign it to a variable. As @Bergi pointed out in the comments, this option does not mutate toppingsSelected

const toppingsSelected = {
  "Onion":"true",
  "Mushrooms":"true",
};
const toppingName = "Onion";
const {
  [toppingName]: topping,
  ...withoutOnion
} = toppingsSelected;

console.log(JSON.stringify(withoutOnion));

To set the React state, you'd then do this

this.setState({ toppingsSelected: withoutOnion })
Sign up to request clarification or add additional context in comments.

7 Comments

Wow, what interesting way of doing this. The point is defining it as undefined I think.
Onion will be undefined in withoutOnion in this example
Yes I know but I can swear it, first I tried it with undefined instead of a random variable and it worked :) Now it does not work. Weird and again very interesting way.
It works for me. deleteTopping = toppingName => { const { [toppingName]: topping, ...withoutOnion } = this.state.toppingsSelected; this.setState({toppingsSelected: withoutOnion}) }
This works @devserkan - must be me confusing the JS syntax again. Thanks.
|
2

You can use delete e.g.

delete toppingsSelected[toppingName];

3 Comments

That works! I tried that before but it didn't work. Must have been using wrong syntax,
Notice that this - unlike destructuring - will mutate toppingsSelected, which is probably a no-go for dealing with state.
Yes, if this method will be used it is better to do something like this: const newToppingsSelected = { ...toppingsSelected }; delete newToppingsSelected[toppingName]; this.setState( { toppingsSelected: newToppingsSelected } )
0

One way of doing this is using Array.prototype.filter()

const _obj = {
  'Onion': true,
  'notOnion': false
};

const newObj = Object.keys(_obj)
  .filter(key => key !== 'Onion')
  .reduce((acc, cur) => ({ ...acc, cur }), {})

console.log(newObj); // { notOnion: false }

This will return a new object without the 'Onion' property

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.