Created
September 30, 2018 20:55
-
-
Save Nicknyr/9c8fc56a41ea6b5bcb623530b3e8aa27 to your computer and use it in GitHub Desktop.
Iterate through 2D array in React
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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