Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why is the .map() function not iterating?

I am trying to iterate through my state via .map(), but the function stops after the first return:

private renderEmployeesTable() {
if (this.state.employees) {


  return new Array(this.state.employees).map((_, index) => {



    const employeeId: string | undefined =
      this.state.employees[index]?.employeeId;
    const employeeName: string | null = this.state.employees[index]?.name;
    return (
      <>
        <div className="row">
          <div className="col">
            <p className="rowText">{employeeId}</p>
          </div>
          <div className="col">
            <p className="rowText">{employeeName}</p>
          </div>
          <div className="col">
            <p className="rowText">
              {
                this.state.workingHoursForEveryEmployee.find(
                  (res) => res.employeeId === employeeId
                )?.workingHours
              }
            </p>
          </div>
          <div className="col">
            <p className="rowText">{this.state.hoursToWork}</p>
          </div>
        </div>{" "}
      </>
    );
  });
}

}

In the console, index is 0 and doesn’t go up.
Do you have any idea, how I could fix this?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Your are creating a new Array() which contains only 1 item: an array.

You probably don’t need the new Array() part, just map this.state.employees directly.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading