0

I'm trying to make a simple loop, so I can log the data in the console:

const demands = data.myDemand;
  console.log(demands);
//First approach
demands.map((demand) => {
  console.log(demand);
});
 //Second approach
 Array.prototype.forEach.call(demands, (demand) => {
   console.log(demand);
 });

Using the first approach I receive the following error:

TypeError: demands.map is not a function

And the second approach just displays nothing. What could be the problem? Logging the whole demands data in the console, I receive this output:

enter image description here

The general Idea is that I want to map this data that's coming from the backend to the props in the current react component but I am not able to get the mapping to works since I need to go through this array of objects, in which some of the objects also contain array of objects...

IvanNickSim
  • 126
  • 3
  • 13
  • 5
    Seems like `demands` is not an array. Based on the debugging screenshot, `demands` is an object. – Ouroborus Aug 03 '21 at 07:39
  • 4
    It isn't an array. You can tell because when you log it it has `{}` around it and not `[]`. – Quentin Aug 03 '21 at 07:39
  • 4
    Yours `demands` seems to be a Map (object) and not an array of objects. You can try `Object.values(demands).map(demand => console.log(demand))` – George Karanikas Aug 03 '21 at 07:39

0 Answers0