0

I am building a react app which will display courses and authors, both courses and authors array's i have to display the list of courses and their corresponding authors. i have used the array map method to map over all courses and display them but i am getting stuck how to display the corresponding authors. below is my code

    import React from "react";
import PropTypes from "prop-types";
import { Link } from "react-router-dom";
import { toast } from "react-toastify";

function CourseList(props) {
  return (
    <table className="table">
      <thead>
        <tr>
          <th>&nbsp;</th>
          <th>Title</th>
          <th>Author Name</th>
          <th>Category</th>
        </tr>
      </thead>
      <tbody>
        {props.courses.map(course => {
          return (
            <tr key={course.id}>
              <td>
                <button
                  className="btn btn-outline-danger"
                  onClick={() => {
                    props.deleteCourse(course.id);
                    toast.success("Course Deleted");
                  }}
                >
                  Delete
                </button>
              </td>
              <td>
                <Link to={"/course/" + course.slug}>{course.title}</Link>
              </td>
              <td>{props.authors.name}</td>
              <td>{course.category}</td>
            </tr>
          );
        })}
      </tbody>
    </table>
  );
}

CourseList.propTypes = {
  deleteCourse: PropTypes.func.isRequired,
  courses: PropTypes.arrayOf(
    PropTypes.shape({
      id: PropTypes.number.isRequired,
      title: PropTypes.string.isRequired,
      authorId: PropTypes.number.isRequired,
      category: PropTypes.string.isRequired
    })
  ).isRequired
};

export default CourseList;

below is my response Json enter image description here

could anyone please help me in displaying the author name in author name cell.

1
  • What is props.authors.name? Shouldn't it be course.authors.name? Commented Mar 12, 2020 at 3:43

2 Answers 2

1

well.. looking at your JSON response.. this could work.

<td>{props.authors.find(author => author.id === course.authorId).name}</td>
Sign up to request clarification or add additional context in comments.

Comments

0

Change <td>{props.authors.name}</td> to <td>{course.authors.name}</td>

don't need to use props for that because you are accessing from array so just access object field of array.

If course.authors.name not found just console your author name and then access that

5 Comments

in course array there is no name elements, authors are present in authors array
@ZahidHussain share your json response.
This is my authors data [ { "id": 1, "name": "Cory House" }, { "id": 2, "name": "Scott Allen" }, { "id": 3, "name": "Dan Wahlin" } ]
is something like this possibe let courses = [...props.courses]; courses.map(course => { course.authorName = "zahid Hussain"; return course; }); but i am using the hardcoded name instead of that i have to get it from authors array
@ZahidHussain edit your post and share your json response not what you have done

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.