Displayusers with an inline array containing the new user object
Here is an example of how you can display users with an inline array containing the new user object:
const users = [
{ id: 1, name: 'John Doe', email: '[email protected]' },
{ id: 2, name: 'Jane Doe', email: '[email protected]' },
{ id: 3, name: 'New User', email: '[email protected]' }
];
const newUserData = { id: 4, name: 'New User 2', email: '[email protected]' };
const updatedUsers = [...users, newUserData];
console.log(updatedUsers);
This will output:
[
{ id: 1, name: 'John Doe', email: '[email protected]' },
{ id: 2, name: 'Jane Doe', email: '[email protected]' },
{ id: 3, name: 'New User', email: '[email protected]' },
{ id: 4, name: 'New User 2', email: '[email protected]' }
]
As you can see, the newUserData
object is added to the end of the users
array using the spread operator (...
). This creates a new array updatedUsers
that contains all the original users plus the new user.
You can also use the concat
method to achieve the same result:
const updatedUsers = users.concat([newUserData]);
This will also output the same result as above.