gpt4 book ai didi

javascript - array.splice() 删除错误的元素

转载 作者:行者123 更新时间:2023-12-01 00:05:11 26 4
gpt4 key购买 nike

我是 React js 的初学者,想知道为什么 splice() 方法对我来说不能正常工作。我的方法在这里:

removePara = index => {
let array = [...this.state.paras];
for (let x = 0; x < this.state.count; x++) {
try {
if (array[x].props.id == index) {
array.splice(x, 1);
}
} catch (e) {}
}
this.setState({ paras: array });
};

所以我传递一个索引,如果元素的 id 与索引匹配,则删除该元素。我使用了 try-catch 因为并非所有索引都在那里(它们可能已通过相同的函数被删除)。这是三个元素的图像: enter image description here

我按下中间的删除键,但发生了这种情况: enter image description here

最后一个被删除,而不是第二个被删除。我该如何解决?我知道我的按钮的 onclick 工作正常。完整代码如下:

//index.js
const rootElement = document.getElementById("root");

class App extends React.Component {
state = {
paras: [],
count: 0
};

addPara = () => {
this.setState({
paras: [
...this.state.paras,
<Paragraph id={this.state.count++} remove={this.removePara} />
]
});
};

removePara = index => {
let array = this.state.paras.filter(e => e.props.id !== index);
this.setState({ paras: array });
};

render() {
return (
<div>
{this.state.paras}
<div className="btn btn-warning" id="add" onClick={this.addPara}>
Add Paragraph
</div>
<div name="count">{this.state.count}</div>
</div>
);
}
}

ReactDOM.render(<App />, rootElement);

//Paragraph.js
class Paragraph extends React.Component {
render() {
let style = {
height: "150px",
marginBottom: "10px"
};

return (
<div className="paragraph" id={this.props.id}>
<div className="row">
<div className="col-sm-11">
<textarea
className="form-control"
name={"paragraph" + this.props.id}
placeholder="Put Paragraph Here"
style={style}
required
></textarea>
</div>
<div className="col-sm-1">
<div
className="btn btn-danger del-rotate"
onClick={() => this.props.remove(this.props.id)}
>
&times;
</div>
</div>
</div>
</div>
);
}
}
<!--index.html-->
<html>

<head>
<script type="application/javascript" src="../reactjs/react.js"></script>
<script type="application/javascript" src="../reactjs/react-dom.js"></script>
<script type="application/javascript" src="../reactjs/babel.js"></script>
<link rel="stylesheet" type="text/css" href="../css/styles.css">
<script src="../bootstrap/js/bootstrap.min.js"></script>
<link rel="stylesheet" type="text/css" href="../bootstrap/css/bootstrap.min.css">
<script type="text/babel" src="Components/Paragraph.js"></script>
<script type="text/babel" src="index.js"></script>
</head>

<body id="body">
<div class="container-fluid">
<br>
<br>
<div class="row">
<div class="col-sm-1"></div>
<div class="col-sm-10">
<div align="center">
<h1>Write Article</h1>
</div>
<br>
<form action="submit-article.php" method="POST">
<input class="form-control input-sm" type="text" placeholder="Enter Title" name="title"
style="width: 35%" autofocus required></input>
<br>
<div id="root"></div>
<br>
<input class="btn btn-success float-right" type="submit" value="Done">
</form>
</div>
</div>
</div>
</body>

</html>

但是与 id 匹配的元素被删除,就像在元素中一样,它不在那里,但被删除的元素的输入只是进入另一个元素。就像上面图片中的示例

最佳答案

您传递的索引可能不正确,

Array.filter 更适合这种任务:

removePara = index => {
let array = this.state.paras.filter(e => e.props.id !== index);
this.setState({ paras: array });
};

关于javascript - array.splice() 删除错误的元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60436621/

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