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"> <title>Array of Objects Demo</title> <script> let arrayOfObjects = [ { name: 'John', role: 'Developer' }, { name: 'Andrew', role: 'Writer' } ]; function displayArray() { let newName = prompt("Enter the name:"); let nameExists = arrayOfObjects.some(obj => obj.name === newName); if (nameExists) { alert("Name already exists. Cannot add duplicate names."); return; } arrayOfObjects.push({ name: newName, role: 'Cricketer' }); let output = document.getElementById('output'); output.innerHTML = JSON.stringify(arrayOfObjects); } </script> </head> <body> <h2>Array of Objects Demo</h2> <p id="output"> [ { name: 'John', role: 'Developer' }, { name: 'Andrew', role: 'Writer' } ];</p> <button onclick="displayArray()">Add Property to Array of Object</button> </body> </html>