I have some user input, I need to split them into characters, then just using map to return both the character array and index array as an object. Try many ways but all failed. Hope I can get some helps here. I am new to react.js and my code may sounds dum. The code is in App.js the main component.
charList = () => {
this.state.userInputs.split("").map((ch1, index1) =>
{ return {ch: ch1, index: index1}
} );
}
I want to be able to access this.charList.ch and this.charList.index for the two arrays. Not sure I described my question clearly. Thanks in advance.
.map((ch, index) => ({ ch, index }))would work as well. Array.prototype.map takes a function that receives array item, index and full array and returns the mapped item so[1,2].map(i=>i*3)the functioni=>i*3receives 1 and then 2 and will return 3 and 6 resulting in[3,6]