screen_rotation
Copied to Clipboard
<!DOCTYPE html> <html> <head> <meta charset="UTF-8" /> <script src="https://unpkg.com/react@17.0.2/umd/react.development.js"></script> <script src="https://unpkg.com/react-dom@17.0.2/umd/react-dom.development.js"></script> <script src="https://cdn.jsdelivr.net/npm/@babel/standalone@7.14.6/babel.min.js"></script> </head> <body> <div id="app"></div> <script type="text/babel" data-type="module"> const { useState } = React; const App = () => { const data = [ { id: 1, name: "John" }, { id: 2, name: "Jane" }, { id: 3, name: "Bob" }, ]; const indexToAccess = 1; let nameAtIndex; if (indexToAccess < data.length) { nameAtIndex = data[indexToAccess].name; } else { nameAtIndex = "Index out of range."; } return ( <div className='container'> <h1>React Js Access Array of object by Index Example</h1> <p>Data: {JSON.stringify(data)}</p> <p> Name at index {indexToAccess}: {nameAtIndex} </p> </div> ); }; ReactDOM.render(<App />, document.getElementById("app")); </script> <style> body { margin: 0; } .container { max-width: 800px; margin: 0 auto; padding: 20px; font-family: Arial, sans-serif; box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1); position: relative; } </style> </body> </html>