我点击产品上的“删除”图标。我取出它的索引并将其保存在状态中,例如:
从数组
在实际应用中,它是什么样子的?给你产品和颜色的ID?我希望它是动态的。我点击产品,下载其索引并删除。
select: 1
,index: 1
。
如何设置this.setState
以删除嵌套在数组products
的数组colors
中的对象。例如,删除对象:{
a: 'orange'
}
从数组
products
中的colors
中删除
this.state.select
是products
中的项目,“this.state.index
”是要删除的项目中的颜色。在实际应用中,它是什么样子的?给你产品和颜色的ID?我希望它是动态的。我点击产品,下载其索引并删除。
class App extends Component {
constructor(){
super();
this.state {
products: [
{
colors: [{a:'black'}, {a:'orange'}, {a:'purple'}]
desc: 'gfgfg'
},
{
colors: [{a: 'yellow'}, {a: 'white'}, {a:'gray'}],
desc: 'gfgfgfg'
},
{
colors: [{a: 'pink'}, {a: 'brown'}, {a:'green'}],
desc: 'gfgfgfg'
}
],
select: 1 //example
index: 1 //example
}
}
removeItem = () => {
const { select, index } = this.state;
if(index) {
this.setState({
products: [
...this.state.products[select].colors.slice(0, index),
...this.state.products[select].colors.slice(index + 1),
]
});
}
};
render () {
return (
<div>
<ul>
{
this.state.products
.map((product, index) =>
<Product
key={index}
index={index}
product={product}
/>
)
}
</ul>
<Products
/>
</div>
)
}
}
this.state.select
是要被删除的产品中的项目,而this.state.index
是要被删除的colors
下的项目? - Christhis.state.select
是产品中的项,this.state.index
是要删除的项中的颜色。 - Umbro