Taking a look at the code below, is there a better way of getting the count of items that contain a certain key/value pair inside of a react state?
This method seems like it could cause a bottleneck once the list that I'm going through becomes large.
Here's a simplified example of the question at hand:
class App extends React.Component {
constructor() {
super();
this.state = {
animals: [
{type: 'cat'},
{type: 'dog'},
{type: 'cat'},
]
};
}
render() {
return(
<div className="app">
<Categories state={this.state} />
</div>
);
}
}
class Categories extends React.Component {
constructor() {
super();
this.countItems = this.countItems.bind(this);
}
countItems(type) {
var count = 0;
for(var i = 0; i < this.props.state.animals.length; i++) {
if(this.props.state.animals[i].type === type) {
count++;
}
}
return count;
}
render() {
return(
<div className="categories">
<div>Total animals: {this.props.state.animals.length}</div>
<div>Cats: {this.countItems('cat')}</div>
<div>Dogs: {this.countItems('dog')}</div>
</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"></div>