Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

78
Views
How to loop over response object with nested array

This is my response. that i am getting

{status: 'Success', length: 7, data: {…}}

This is an expanded version.

data:
tours: (7) [{…}, {…}, {…}, {…}, {…}, {…}, {…}]
[[Prototype]]: Object
length: 7
status: "Success"

I wanted to loop over the tour property but doing a map is not going to work. I tried doing Object.entries() to convert it into an array but it is also not working. Can anyone suggest me a solution?

export const getData = () => {
  return async (dispatch) => {
    try {
      dispatch(isLoading(true));
      const res = await fetch(`http://localhost:5555/api/bc1/tours`);

      if (!res.ok) {
        throw new Error(`Error while connecting with server`);
      }

      const data = await res.json();
      dispatch(isLoading(false));
      dispatch(getTours(data));
    } catch (error) {
      dispatch(
        checkStatus({
          title: `Error`,
          message: error.message,
        })
      );
    }
  };
};

getTours is an empty array.

calling it in app.js

const dispatch = useDispatch();
  useEffect(() => {
    dispatch(getData());
  }, [dispatch]);
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

No need to convert tours to object. You can do this:

const result = {status: 'Success', length: 7, data: {…}};
result.data.tours.map(function(item, i){
  // your code
})
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!