gpt4 book ai didi

javascript - 如何根据 React 嵌套数组中的索引删除对象?

转载 作者:行者123 更新时间:2023-11-30 19:28:49 25 4
gpt4 key购买 nike

我单击产品上的“删除”图标。我拉出他的索引并将其保存在状态中。示例:选择:1索引:1。如何设置 this.setState 以删除嵌套在数组 products 中的数组 colors 中的对象。示例删除对象:

{
  a: 'orange'
}

从数组 colors 到数组 products

this.state.select 是products中的item,this.state.index是item中要删除的color

它在实际应用中的表现如何?给你的产品和颜色 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>
)
}
}

最佳答案

需要将remove函数传递给Product组件,在Product组件中将select和index传递给removeItem函数。

修改您的删除项目,接受两个参数,selectindex

removeItem = (select, index) => {

const filtered = this.state.products[select].colors.filter(
(color, i) => i !== index
);

this.setState(prevState => {
return {
select: select,
index: index,
products: [
...prevState.products.slice(0, select),
Object.assign({}, prevState.products[select], { colors: filtered }),
...prevState.products.slice(select + 1)
]
};
});
};

将函数作为 prop 传递给 Product 组件。

<div>
<ul>
{this.state.products.map((product, index) => (
<Product
key={index}
index={index}
removeItem={this.removeItem}
product={product}
/>
))}
</ul>
</div>

在您的产品组件中,传递颜色索引和选择。

<button onClick={() => removeItem(index, i)}>X</button>

演示

<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.0/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.0/umd/react-dom.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/babel-standalone/6.21.1/babel.min.js"></script>

<div id="root"></div>

<script type="text/babel">

class Product extends React.Component {
render() {
const { product, removeItem, index } = this.props;
return (
<div>
<p>{product.desc}</p>
<ul>
{product.colors.map((color, i) => (
<li>
{color.a} <button onClick={() => removeItem(index, i)}>X</button>
</li>
))}
</ul>
</div>
);
}
}

class App extends React.Component {
constructor() {
super();
this.state = {
name: "React",
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 = (select, index) => {
const filtered = this.state.products[select].colors.filter(
(color, i) => i !== index
);

this.setState(prevState => {
return {
select: select,
index: index,
products: [
...prevState.products.slice(0, select),
Object.assign({}, prevState.products[select], { colors: filtered }),
...prevState.products.slice(select + 1)
]
};
});
};

render() {
return (
<div>
<ul>
{this.state.products.map((product, index) => (
<Product
key={index}
index={index}
removeItem={this.removeItem}
product={product}
/>
))}
</ul>
</div>
);
}
}

ReactDOM.render(<App />, document.getElementById("root"));
</script>

关于javascript - 如何根据 React 嵌套数组中的索引删除对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56650591/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com