1

I separated map and now I'm confused how to get the index.

let typeList = [];
        for (let item of items) {
            if (typeList.indexOf(item.status) < 0) {
                typeList.push(item.status);
            }
        }

const renderList = (status) => {
   //how to get the index here?
}

return(
   { typeList.map(renderList) }
)
Mayank Shukla
  • 100,735
  • 18
  • 158
  • 142
Alex Yong
  • 7,425
  • 8
  • 24
  • 41
  • Please [use the search](https://stackoverflow.com/search?q=%5Bjavascript%5D+map+get+index) before you as a new question. – Felix Kling Mar 02 '17 at 18:34

1 Answers1

0

Write it like this:

const renderList = (status, i) => {
   //how to get the index here?
   console.log('status', status, i)
}

return(
   typeList.map(renderList)
)

Check this example:

function fun(item, i){
  console.log('item', item, i);
}

a = [1,2,3,4,5];

a.map(fun);

Check the react component:

class App extends React.Component { 

    _createList(item, index) {
       return <div>{item}: {index}</div>
    }

    render() {
        return (
            <div>
                {[1,2,3,4,5].map(this._createList)}
            </div>
        )
    }
}


ReactDOM.render(<App />, document.getElementById('container'));
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>

<div id='container'>
Mayank Shukla
  • 100,735
  • 18
  • 158
  • 142