0

I'm making an app in react and I don't know why when I'm using for-in loop that returns only the first object, not all of them. My expected output is like in arr.map() but unfortunately I'm not able to that using objects instead of arrays.

places: {
  place_1: {
    title: 'Place 1',
  },
  place_2: {
    title: 'Place 2',
  },
  place_3: {
    title: 'Place 3',
  },
}


const App = () => {

 const handleMenu = () => {
    for (const key in places) {
      if (Object.hasOwnProperty.call(places, key)) {
        return (
          <h1>
            title={places[key].title}
          </h1>
        );
      }
    }
  };

  return (
    <div>
      {handleMenu()}
    </div>
  )
}

export default App
Stairss
  • 176
  • 1
  • 14

0 Answers0