Javascript 从字典中删除项目的最佳方法
我的代码中有一个名为CarValues的字典,其中包含以下数据: CarValues是在状态下初始化的字典Javascript 从字典中删除项目的最佳方法,javascript,reactjs,react-native,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
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
我想从CarValues中完全删除本田及其价值。虽然我很少看到类似的问题,但我找不到这个问题的最佳解决方案
我相信,为了真正做到这一点而不改变状态,您需要像这样重新创建整个状态
class Test extends React.Component {
state = {
thingToDelete: {},
otherStuff: {}
};
deleteThingToDelete = () => {
const {thingToDelete, ...state} = this.state;
this.setState(state);
}
}
使用spread操作符,我们实现了一个浅层克隆,所以要小心。另一个选项是使用Object.assign,但这也只能提供一个浅克隆,但您将获得更好的浏览器支持。我相信,为了真正做到这一点而不改变状态,您需要像这样重新创建整个状态
class Test extends React.Component {
state = {
thingToDelete: {},
otherStuff: {}
};
deleteThingToDelete = () => {
const {thingToDelete, ...state} = this.state;
this.setState(state);
}
}
使用spread操作符,我们实现了一个浅层克隆,所以要小心。另一个选项是使用Object.assign,但这也只能提供浅层克隆,但您将获得更好的浏览器支持。这将解决您的问题
yourMethod(key) {
const copyCarValues= {...this.state.CarValues}
delete copyCarValues[key]
this.setState({
CarValues: copyCarValues,
})
}
这会解决你的问题
yourMethod(key) {
const copyCarValues= {...this.state.CarValues}
delete copyCarValues[key]
this.setState({
CarValues: copyCarValues,
})
}
最简单的方法是:
const carValues = Object.assign({}, this.state.carValues)
delete carValues[key]
this.setState({ carValues })
最简单的方法是:
const carValues = Object.assign({}, this.state.carValues)
delete carValues[key]
this.setState({ carValues })
可以使用下划线.js或Lodash
可以使用下划线.js或Lodash 首先全局初始化数组 将对象添加到字典中 从字典更新对象 从字典中删除对象 首先全局初始化数组 将对象添加到字典中 从字典更新对象 从字典中删除对象
这里有另一个足够简单的解决方案来实现这一点
const myCarsValueInState = this.state.myCarsValueInState;
Object.keys(myCarsValueInState).map((index) => {
myCarsValueInState[index] = undefined; // you can update on any condition if you like, this line will update all dictionary object values.
return myCarsValueInState;
});
很简单 这里有另一个足够简单的解决方案来实现这一点
const myCarsValueInState = this.state.myCarsValueInState;
Object.keys(myCarsValueInState).map((index) => {
myCarsValueInState[index] = undefined; // you can update on any condition if you like, this line will update all dictionary object values.
return myCarsValueInState;
});
很简单 对于删除对象(字典)属性,可以使用
delete obj[attr]
。如果您的问题是关于更改React组件状态,那么您应该添加状态结构并进一步解释问题所在。至于删除对象(字典)属性,您可以使用delete obj[attr]
。如果您的问题是关于更改React组件状态,那么您应该添加您的状态结构,并进一步解释问题是什么。它不应该是delete copyCarValues[key]
而不是delete CarValues[key]
?我有一个问题:不会删除此.state.CarValues[this.state.key]直接解决它。为什么我们必须创建副本并删除该项,然后更新状态。对不起,我是新的前端和用户界面的东西。请原谅我的问题,如果它是跛脚的。Reacts状态是不可变的,这意味着您永远不会更改状态,如果您想进行更改,您将替换整个状态@Vicky不应该是删除copyCarValues[key]
而不是删除CarValues[key]
?我有一个问题:不会删除这个.state.CarValues[this.state.key]直接解决它。为什么我们必须创建副本并删除该项,然后更新状态。对不起,我是新的前端和用户界面的东西。请原谅我的问题,如果它是跛脚的。Reacts状态是不可变的,这意味着您永远不会更改状态,如果您想进行更改,您将替换整个状态@维姬
const myCarsValueInState = this.state.myCarsValueInState;
Object.keys(myCarsValueInState).map((index) => {
myCarsValueInState[index] = undefined; // you can update on any condition if you like, this line will update all dictionary object values.
return myCarsValueInState;
});