#How do I change the value of a multiple keys?

27 messages · Page 1 of 1 (latest)

uncut plaza
#

``const users = [
{
_id: 'ab12ex',
username: 'Alex',
email: 'alex@alex.com',
password: '123123',
createdAt:'08/01/2020 9:00 AM',
isLoggedIn: false
},
];

function makeid(length) {
let result = '';
const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
const charactersLength = characters.length;
let counter = 0;
while (counter < length) {
result += characters.charAt(Math.floor(Math.random() * charactersLength));
counter += 1;
}
return result;
}

function signUp([Email, username, password]){
if(users.find(Email=>users.email===Email)===true){
console.log('You alreaday have an account. Please head to the signin page instead.')
}
else{
let u = Object.assign({}, user[0])
users.push(u)

    return("You've made a new profile.")
}

}

console.log(signUp(["user@user.com", "user", "231235"]))``

In the program what I'm trying to do is take one of the objects from the array and modify it so that it equals the email and stuff I set for it.

#

or would it be more effective to just make a new object without copying the existing one?

crimson hound
#

To change the value of multiple keys, you can use the dot notation or square bracket notation to access the object and modify the key value pairs. Here's an example:

const user = {
  username: 'JohnDoe',
  email: 'johndoe@example.com',
  password: '123456',
  isLoggedIn: false
};

// Change multiple key value pairs
user.username = 'JaneDoe';
user.password = 'abcdef';
user.isLoggedIn = true;

console.log(user); // Output: { username: 'JaneDoe', email: 'johndoe@example.com', password: 'abcdef', isLoggedIn: true }

In your code example, since you're using Object.assign() to create a new object from an existing one, modifying multiple keys may be more effective.

#

The original object intact, it would be better to create a new object without copying the existing one.

#

HOPE IT HELPS

uncut plaza
#

thanks a lot!!

#

I'm going to try this real quick

#

now umm, I have one last question. This works, but once I exit vsc the array will reset. So how do I make it part of the array permanently so that the user can return once making the account?

#

I was planning on using this for a simple website or something

crimson hound
#

Ok DM me with the code and what you want to do with it

crimson hound
#
  • To permanently store the users array, you need to write it to a file. You can use Node.js built-in module fs to write to a file. Here's one approach to do that:
  1. Create a separate module that exports a function to write the users array to a file. For example, let's call the module writeUsersToFile.js:
const fs = require('fs');

function writeUsersToFile(users) {
  fs.writeFile('users.json', JSON.stringify(users), (err) => {
    if (err) {
      console.error(err);
    } else {
      console.log('Users file has been updated.');
    }
  });
}

module.exports = writeUsersToFile;

This function takes the users array as a parameter, converts it to a JSON string using JSON.stringify(), and writes it to a file called users.json.

  1. Import the writeUsersToFile() function in your main
uncut plaza
uncut plaza
uncut plaza
crimson hound
#

Ok I will help you out with that one because I have a good idea in AI I will start writing the code right away if you have anything to ask or say then message me 🙂

crimson hound
#

I will send you the code privately ok

uncut plaza
#

Idk when I'll start working on it

#

the html part

#

at least

crimson hound
#

Ok

proven crown
#

!warn @crimson hound Stop asking people to DM you...

spring vergeBOT
#
Mod Warning

@crimson hound Stop asking people to DM you...

uncut plaza
proven crown
#

!rule DM