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://cdnjs.cloudflare.com/ajax/libs/babel-standalone/6.26.0/babel.min.js"></script> </head> <body> <div id="app"></div> <script type="text/babel"> const { useState } = React; function App() { const arrayOfObjects = [ { id: 1, name: 'John' }, { id: 2, name: 'Jane' }, { id: 3, name: 'Bob' }, ]; const checkValueExists = (value) => { return arrayOfObjects.some((obj) => obj.name.toLowerCase() === value.toLowerCase()); }; const [inputValue, setInputValue] = useState('bob'); const [result, setResult] = useState(null); const handleInputChange = (e) => { setInputValue(e.target.value); }; const handleCheckExists = () => { const exists = checkValueExists(inputValue); setResult(exists ? `${inputValue} exists` : `${inputValue} does not exist`); }; return ( <div className='container'> <h3>React JS Check array of object containse specfic value</h3> <input type="text" value={inputValue} onChange={handleInputChange} placeholder="Enter a value" /> <button onClick={handleCheckExists}>Check existence</button> {result && <p>{result}</p>} </div> ); } ReactDOM.render(<App />, document.getElementById("app")); </script> <style> * { box-sizing: border-box; } .container { margin: 0 auto; width: 600px; text-align: center; box-shadow: 0 1px 2px 0 rgba(0, 0, 0, 0.12), 0 2px 4px 0 rgba(0, 0, 0, 0.24); padding: 20px; } h3 { font-size: 24px; margin-bottom: 20px; } input[type="text"] { padding: 10px; margin-bottom: 10px; width: 200px; border: 1px solid #ccc; border-radius: 5px; } button { padding: 10px 20px; background-color: #4caf50; color: #fff; border: none; border-radius: 5px; cursor: pointer; } button:hover { background-color: #45a049; } p { margin-top: 20px; font-size: 18px; text-align: center; } </style> </body> </html>