Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get current index of an item with Object.entries().map

Tags:

javascript

I know it is probably a silly question but I can' find an answer by myself, so..

How can I get current index with this construction .map(([key, value])?

to access it inside the map function bellow

     return (
        <Fragment>
            {Object.entries(props.values).map(([key, value]) => {
                return (
                    <Button
                        key={key}
                        value={value}                      
                    />
                );
            })}
        </Fragment>
     );
like image 692
Andrew Avatar asked Nov 05 '25 05:11

Andrew


1 Answers

You can obtain an index from callback in map

 return (
    <Fragment>
        {Object.entries(props.values).map(([key, value], index) => {
            return (
                <Button
                    key={key}
                    value={value}                      
                />
            );
        })}
    </Fragment>
 );

Please check https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map

like image 76
grzim Avatar answered Nov 06 '25 21:11

grzim



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!