Answer the question
In order to leave comments, you need to log in
How to keep the original index of an array when concatenating?
There is a table with a list of students, there is also a form for searching by last name. The problem is that after the search, in the table the serial number of the found student is 1, and not the original number before the search.
The code below is used to search for students by last name.
searchStudent(lastname) {
if (lastname !== "") {
this.dropStudents();
const group = this.state.currentGroup;
let arr = [];
for (let i = 0; i < group.length; i++) {
if (lastname.toLowerCase() === group[i].lastname.toLowerCase()) {
arr = arr.concat([group[i]]);
}
}
this.setState({ currentStudents: arr });
} else if (lastname === "") {
this.dropStudents();
this.setState({ currentStudents: this.state.currentGroup });
}
}
dropStudents() {
this.setState({ currentStudents: [] });
}
if (this.props.currentGroup.length !== 0) {
const currentGroup = this.props.currentGroup;
return (
<section className="json-teachers">
<table><tbody>
{
currentGroup.map((student,index) => {
return (
<tr key={index} className="line">
<td>{index + 1}</td>
<td>{student.name}</td>
<td>{student.lastname}</td>
<td>{student.status}</td>
</tr>
);
})
}
</tbody></table>
</section>
);
} else { return null;}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question