'Write a function which editUser if the user exist in the users array?
I am using the array.map higher order function with a ternary operator to check the condition. I am not familiar with the method Object.assign
I tried a solution for the question, but it gives no return in the console, please verify and rectify my approach.
const users = [
{
name:'Brook',
scores:75,
skills:['HTM', 'CSS', 'JS'],
age:16
},
{
name:'Alex',
scores:80,
skills:['HTM', 'CSS', 'JS'],
age:18
},
{
name:'David',
scores:75,
skills:['HTM', 'CSS'],
age:22
},
{
name:'John',
scores:85,
skills:['HTM'],
age:25
},
{
name:'Sara',
scores:95,
skills:['HTM', 'CSS', 'JS'],
age: 26
},
{
name:'Martha',
scores:80,
skills:['HTM', 'CSS', 'JS'],
age:18
},
{
name:'Thomas',
scores:90,
skills:['HTM', 'CSS', 'JS'],
age:20
}
]
My solution: (NodeJS)
const editUser = (users, userUpdate) => {
return users.map((user) => user.name == userUpdate ? Object.assign(user, { scores: userUpdate.scores, skills: userUpdate.skills, age: userUpdate.age }) : user);
}
Execution:
const userUpdate = {
name: 'Alex',
scores: 88,
skills: ['HTM', 'CSS', 'JS', 'FullStack'],
age: 20
}
console.log(editUser(users, userUpdate));
Solution 1:[1]
userUpdate is object that contain name property and must be compair user.name with userUpdate.name
in this problem you compair object with name and is not equal.
const editUser = (users, userUpdate) => {
return users.map((user) => user.name == userUpdate.name ? Object.assign(user, { scores: userUpdate.scores, skills: userUpdate.skills, age: userUpdate.age }) : user);
};
Solution 2:[2]
We sometimes use object.asign() for clone an Object in JavaScript (without reference)
let user = { name: 'Alex'}
let newUser = user;
newUser.name = 'John';
console.log(newUser); //{ name: 'John' }
console.log(user); // { name: 'John' }
If you want to clone an object without reference you can use Object.assign() or three dots (...) is called spread operator in Javascript
I prefer three dots because it's a feature of ECS6.
Using of Object.assign():
let user = { name: 'Alex'}
let newUser = Object.assign({}, user);
newUser.name = 'John';
console.log(newUser);// { name: 'John' }
console.log(user);//{ name: 'Alex' }
Using three dots (...) or spread operator
let user = { name: 'Alex'}
let newUser = {...user};
newUser.name = 'John';
console.log(newUser); // { name: 'John' }
console.log(user); //{ name: 'Alex' }
You could visit this tutorial for more examples
For merging objects using Object.assign() or 'sperate operator', please check this tutorial
Answer your question is:
I think you don't need Object.assign() because keys of users and userUpdated is similar so just try like this:
const editUser = (users, userUpdate) => {
return users.map((user) => user.name == userUpdate.name ? userUpdate: user);
};
Solution 3:[3]
You can use Array.prototype.findIndex() to get the user's index within the array with userUpdate.name
- The advantage is that in case of many users the search will stop immediately when the user is found
So you can modify that user if index is greater than 0
Code:
const users = [{name: 'Brook',scores: 75,skills: ['HTM', 'CSS', 'JS'],age: 16,},{name: 'Alex',scores: 80,skills: ['HTM', 'CSS', 'JS'],age: 18,},{name: 'David',scores: 75,skills: ['HTM', 'CSS'],age: 22,},{name: 'John',scores: 85,skills: ['HTM'],age: 25,},{name: 'Sara',scores: 95,skills: ['HTM', 'CSS', 'JS'],age: 26,},{name: 'Martha',scores: 80,skills: ['HTM', 'CSS', 'JS'],age: 18,},{name: 'Thomas',scores: 90,skills: ['HTM', 'CSS', 'JS'],age: 20,}]
const userUpdate = {
name: 'Alex',
scores: 88,
skills: ['HTM', 'CSS', 'JS', 'FullStack'],
age: 20,
}
const editUser = (users, userUpdate) => {
const index = users.findIndex((u) => u.name === userUpdate.name)
if (index > 0) {
users[index] = userUpdate
}
return users
}
const result = editUser(users, userUpdate)
console.log(result)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Moein T |
| Solution 2 | Mohammad Yaser Ahmadi |
| Solution 3 | Yosvel Quintero Arguelles |
