gpt-4o-mini-2024-07-18· OpenAI
WildChat-4.8M (AI2)
node js addfield code
- User
node js addfield code
- Assistant
If you're looking to add a field to an object in Node.js, it's fairly simple. Here’s how you can do it with an example:
Example: Adding a Field to an Object
// Existing object let user = { name: "John Doe", age: 30 }; // Adding a new field user.email = "johndoe@example.com"; console.log(user);Output
{ "name": "John Doe", "age": 30, "email": "johndoe@example.com" }Using a Function
You might also want to create a function to encapsulate the logic of adding a field:
function addField(obj, fieldName, fieldValue) { obj[fieldName] = fieldValue; } let user = { name: "John Doe", age: 30 }; addField(user, 'email', 'johndoe@example.com'); console.log(user);