I am trying to do a simple operation to create an array in React and set it equal to a state object. Anyone know why state.countState is being populated with "null" for each array element? I think I'm missing something very simple here...
class DisplaySort extends Component {
constructor(props) {
super(props);
this.state = {
array: [5, 4, 3, 2, 1],
countState: [],
};
this.sort = this.sort.bind(this);
}
render() {
return (
<div className="buttons">
<button onClick={this.sort}>Sort</button>
</div>
);
}
sort(e) {
let data = this.state.array;
let arr = [...data];
let min = Math.min(...arr);
let max = Math.max(...arr);
let i = min,
j = 0,
len = arr.length,
count = [];
for (i; i <= max; i++) {
count[i] = 0;
}
for (i = 0; i < len; i++) {
count[arr[i]] += 1;
}
this.setState({ countState: count });
};
Math.min([5, 4, 3, 2, 1])returnsNaN.sortdo? It doesn't appear to be sorting the array.