0

In my react app I'm consuming an API that contains inside the JSON response a field called experience which contains an [{},{}]. In my view, I need to show what inside that tarry and I'm trying to use .map(). For example, I want to show a list of experiences.

The API response getting the Experiences is that:

{
  "profileExperiences": [
    {
      "username": "Jakos",
      "experience": [
        {
          "image": "https://via.placeholder.com/150",
          "_id": "5e3975f95fbeec9095ff3d2f",
          "role": "Developer",
          "company": "Google",
          "startDate": "2018-11-09T23:00:00.000Z",
          "endDate": "2019-01-05T23:00:00.000Z",
          "area": "Copenhagen",
          "createdAt": "2020-02-04T13:47:37.167Z",
          "updatedAt": "2020-02-04T13:47:37.167Z"
        },
        {
          "image": "https://via.placeholder.com/150",
          "_id": "5e3978bf5e399698e20c56d4",
          "role": "Developer",
          "company": "IBM",
          "startDate": "2018-11-09T23:00:00.000Z",
          "endDate": "2019-01-05T23:00:00.000Z",
          "area": "Copenhagen",
          "createdAt": "2020-02-04T13:59:27.412Z",
          "updatedAt": "2020-02-04T13:59:27.412Z"
        }
      ],
      "experiences_count": 2
    }
  ]
}

I'm interested in what is inside the expereince[] and I want to show that data in a list.

What I tried is this:

const Experiences = () => {
  const dispatch = useDispatch();
  const { experiences } = useSelector(state => ({
    experiences: state.ExperiencesReducer.experiences
  }));

  console.log("My Exp", experiences.profileExperiences);

  useEffect(() => {
    dispatch(ExperiencesMiddleware.getAllExperiencesProfile(USERNAME));
  }, [dispatch]);

  return (
    <>
      <Container>
        <h1>Experience</h1>

        {experiences.profileExperiences &&
          experiences.profileExperiences.map((E, k) => (
            <Row key={k}>
              <ul>
                <li>{E.experience.role}</li>
              </ul>
            </Row>
          ))}
      </Container>
    </>
  );
};

export default Experiences;

I'm able to debug until {E.expereince.role} and the role is undefined but experience instead contains my 2 obj of the array and I cannot figure out how can I show that list. Do I need an extra mapping for it or need to change approach?

2 Answers 2

2
//Your need one more mapping for experience array  
  {experiences.profileExperiences &&
              experiences.profileExperiences.map((P) =>P.experience).flat().map((E,k)=>(
                <Row key={k}>
                  <ul>
                    <li>{E.role}</li>
                  </ul>
                </Row>)
              )}
Sign up to request clarification or add additional context in comments.

4 Comments

I'm getting this error now: TypeError: (intermediate value).map is not a function
yeah.I will get back to you in minutes with updated answer
I have Edited my answer.If you want all experience to show,it works
Not that it matter much, but don't use the index as the key (key={k}) and instead use the unique ID already available with each item here, E._id.
0

E.experience.role won't work because experience is an array. You will need to either map through those E.experience.map(value => value.role) or reference the correct index E.experience[0].role.

For your usecase maybe something like this would work:

experiences.profileExperiences[0].experience.map((E, k) => (
  <Row key={k}>
    <ul>
      <li>{E.role}</li>
    </ul>
  </Row>
))}

Or if you want to account for multiple profileExperiences:

experiences.profileExperiences.map((E, k) => (
  <>
    <h1>Profile: {E.username}</h1>
    {E.exeriences.map(ex => (
      <Row key={k}>
        <ul>
          <li>{ex.role}</li>
        </ul>
      </Row>
    ))}
  </>
))}

1 Comment

I tried your version but gives map is undefined TypeError: Cannot read property 'map' of undefined

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.