从 Dictionary React js 中删除项目的最佳方法

Vic*_*cky 2 javascript reactjs react-native

我的代码中有一个名为 CarValues 的字典,其中包含以下数据: CarValues 是在状态中初始化的字典。

dictionary: CarValues

key ==> string

Value ==> Array

key => Honda, Value => white, yellow, red, orange
key => Toyota, Value => white, yellow, green, black
Key => Volkswagen Value => 123, 456, 343
Run Code Online (Sandbox Code Playgroud)

我想从 CarValues 中完全删除本田及其价值。虽然,我很少看到类似的问题,但我找不到这个问题的最佳解决方案。

如何从 Reactjs 组件的状态对象中删除属性

sim*_*lor 5

这应该可以解决您的问题

yourMethod(key) {
  const copyCarValues= {...this.state.CarValues}
   delete copyCarValues[key]
  this.setState({
     CarValues: copyCarValues,
  })
}
Run Code Online (Sandbox Code Playgroud)