Skip to content

Instantly share code, notes, and snippets.

@Nicknyr
Created September 30, 2018 20:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Nicknyr/9c8fc56a41ea6b5bcb623530b3e8aa27 to your computer and use it in GitHub Desktop.
Save Nicknyr/9c8fc56a41ea6b5bcb623530b3e8aa27 to your computer and use it in GitHub Desktop.
Iterate through 2D array in React
import React, { Component } from 'react';
import logo from './logo.svg';
import './App.css';
class App extends Component {
constructor(props) {
super(props);
this.state = {
data: [[1,1,0,1,1,1],[1,1,1]]
};
}
componentDidMount = () => {
const data = this.state.data;
var counter = 0;
for(var i = 0; i < data.length; i++) {
for(var j = 0; j < data[i].length; j++) {
counter += data[i][j];
}
}
console.log(counter);
}
render() {
return (
<div className="App">
<header className="App-header">
<img src={logo} className="App-logo" alt="logo" />
<h1 className="App-title">Welcome to React</h1>
</header>
<p className="App-intro">
To get started, edit <code>src/App.js</code> and save to reload.
</p>
</div>
);
}
}
export default App;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment