locked
can i get an index in a map? RRS feed

  • Question

  • User1034446946 posted

    hi

    I have a functional component which use react-lodash Map to iteerate through an object which is a list of objects, but i would like an index in it and I am going round in circles

    return (
       <nav>
           <ul>              
                <IsEmpty
                    value={data}
                    yes="Empty list"
                    no={() => (             
                        <Map collection={data} iteratee={(obj, key) => 
                            <li>{index}</li>
                            } 
                        />
                     )}
                />       
            </ul>
        </nav>
        )

    i know i could use my keys but my keys are descriptive and i would prefer my index to be numeric, any thoughts would be appiciated

    Friday, May 8, 2020 11:40 PM

Answers

  • User-474980206 posted

    just convert the mapped object to an array

    <Map collection={Object.entries(data)} iteratee={(entity, index) => 
         <li>{index} - {entity.key} - {entity.value}</li>
       } 
    />

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, May 11, 2020 2:55 PM

All replies

  • User-474980206 posted

    just convert the mapped object to an array

    <Map collection={Object.entries(data)} iteratee={(entity, index) => 
         <li>{index} - {entity.key} - {entity.value}</li>
       } 
    />

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, May 11, 2020 2:55 PM
  • User1034446946 posted

    thanks

    Tuesday, May 12, 2020 11:25 PM