-4

'''

const users = []

const addUser = ({ id, username, room }) => {
    // Clean the data
    username = username.trim().toLowerCase()
    room = room.trim().toLowerCase()

    // Validate the data
    if (!username || !room) {
        return {
            error: 'Username and room are required'
        }
    }
    // Check for existing user
    const existingUser = users.find((user) => {
        return user.username === username || user.room === room
    })

    // Validate username
    if (existingUser) {
        return {
            error: 'Username already exists!'
        }
    }

    // Store user
    const user = { id, username, room }
    users.push(user)
    return { user }
}

addUser({
    id: 03,
    username: 'rohan',
    room: 'playground'
})
console.log(users)

'''

If I run this in console the output is [ { id: 3, username: 'rohan', room: 'playground' } ]

But again if i just comment out the call and print the array. It showing empty.

'''

//addUser({
//    id: 03,
//    username: 'rohan',
//    room: 'playground'
//})
console.log(users)

'''

From first run the value stored in object so It must be in the users array forever. Why this is empty if I dnt add value?

  • Do not declare any empty values like: `[]`, or `{}`, or `null` with `const` unless you fully understand that declaring empty values such as what was just mentioned cannot be directly changed when defined with `const`. Use `var` or `let`. – zer00ne Mar 01 '20 at 08:46
  • 1
    @zer00ne pushing to a const array is perfectly valid. I just can't reproduce his problem at all. – ASDFGerte Mar 01 '20 at 08:47
  • have you tried putting a [debugger](https://stackoverflow.com/a/25551397/1823841) keyword inside `addUser` and check what is happening at each line of code? – palaѕн Mar 01 '20 at 08:48
  • towards "must be in the users array forever" - it could get deleted at some point in time, but you don't show any code, that would do so. – ASDFGerte Mar 01 '20 at 08:51
  • `addUser()` returns an `user` object wrapped in another object. The `users` array you see is supposed to be...? – zer00ne Mar 01 '20 at 08:55
  • My problem is. Why array is not holding the previous values – RO H AN Mar 01 '20 at 10:03
  • @ROHAN See [my answer](https://stackoverflow.com/a/60473708/2813224). – zer00ne Mar 01 '20 at 10:54
  • When you say you comment out the line, are you rerunning the whole script? You will lose all data if you refresh the page. – Grabofus Mar 01 '20 at 11:36

1 Answers1

0

The following demo features 2 separate objects (userObjA, and userObjB), an arrow function called addUser(...user) which can accept any number of given objects (because of the spread operator: ... magic) then returns said objects wrapped in an array of objects (users).

Note: no const were hurt during the demonstration -- healthy free range let are leveraged for painless declarations and assignments.


Demo

let userObjA = {
  id: 3,
  name: 'Rohan',
  room: 'Suite 42'
};

let userObjB = {
  id: 4,
  name: 'Dell',
  room: 'Single 601'
};

const addUser = (...user) => {
  let users = [];
  users.push(user);
  return users.flat();
};

console.log(addUser(userObjA, userObjB));
Community
  • 1
  • 1
zer00ne
  • 41,936
  • 6
  • 41
  • 68
  • Now my qus is what if you then remove the function call addUser and then print only the users array? The 2 objects will stay in array or not? This is my point – RO H AN Mar 01 '20 at 11:16
  • Please define your version of what "stored/saved" data is. In normal terms a variables value lives as long if not shorter period of time which is the life-cycle of the currently running processes of a function/expression, etc. (ie not long at all). Say `addUser()` just finished running for the first time and it returns an objectArray that it collected from a given group of objects. The next time `addUser()` runs do you expect it to return...? – zer00ne Mar 01 '20 at 11:25
  • *"...But again if i just comment out the call and print the array. It showing empty."* The code you are commenting out is the call to `addUser(user)` that's actually changing the reference to the `user` array. Since commented out then it is only logical to expect the array `user` sits as it did initially that is empty and never changed. – zer00ne Mar 01 '20 at 11:30
  • Use `let` and then `const` for arraow functions. – zer00ne Mar 01 '20 at 11:48