screen_rotation
Copied to Clipboard
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <script src="https://unpkg.com/react@17.0.2/umd/react.production.min.js"></script> <script src="https://unpkg.com/react-dom@17.0.2/umd/react-dom.production.min.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, useEffect } = React; function App() { const stringArray = ["name:John", "age:30", "city:New York"]; const [keyValueObject, setKeyValueObject] = useState({}); useEffect(() => { const updatedKeyValueObject = {}; stringArray.forEach(item => { const [key, value] = item.split(':'); if (key && value) { updatedKeyValueObject[key] = value; } }); setKeyValueObject(updatedKeyValueObject); }, []); return ( <div className='container'> <h3 className='title'>React Js convert string to Key-Value Object</h3> <p className='text'>Array of String{JSON.stringify(stringArray)}</p> <pre className='code'>Key Pair Object : {JSON.stringify(keyValueObject, null, 2)}</pre> </div> ); } ReactDOM.render(<App />, document.getElementById("app")); </script> <style> /* Styles for the container */ .container { padding: 20px; border-radius: 10px; box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1); max-width: 600px; margin: 0 auto; text-align: center; } /* Styles for the title */ .title { font-size: 24px; font-weight: bold; margin-bottom: 10px; color: #333; } /* Styles for the text paragraph */ .text { font-size: 16px; color: #666; margin-bottom: 20px; } /* Styles for the code block */ .code { background-color: #f0f0f0; padding: 15px; border-radius: 5px; font-family: monospace; white-space: pre-wrap; font-size: 14px; color: #333; overflow-x: auto; } </style> </body> </html>