最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Remove an object's key and value using a variable from function - Stack Overflow

programmeradmin2浏览0评论

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?

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?

Share Improve this question asked Aug 2, 2018 at 13:12 ViktorMSViktorMS 1,37117 silver badges31 bronze badges 1
  • As far as I know you can't use dynamic keys while destructing. So, go for alternative provided answer. – devserkan Commented Aug 2, 2018 at 13:19
Add a ment  | 

3 Answers 3

Reset to default 6

Another option is to add square brackets arround toppingName, and assign it to a variable. As @Bergi pointed out in the ments, 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 })

You can use delete e.g.

delete toppingsSelected[toppingName];

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

发布评论

评论列表(0)

  1. 暂无评论