A
A
Artem Pavliienko2018-03-04 12:32:33
JavaScript
Artem Pavliienko, 2018-03-04 12:32:33

How to filter an object by username?

Hello.
I write applications in Angular with a registration form. The data is all stored in localStorage. I can't check for the username, i.e. if there is one, then we display messages, if not, then we write to the array. Please tell me how to do the check and what would be written to localStarage normally. Neither now records with a third object, but at the next registration it changes this object in place of what would be recorded by another one.
Here is the code:

registration(event) {
        event.preventDefault();

        this.localStor();

        let newUserName = event.target.form.elements[0].value,
            newUserPass = event.target.form.elements[1].value;

        // берем значения с локал и сразу преобразуем
        let meaningsLocalS = JSON.parse(localStorage.getItem('user'));

        for (var prop in meaningsLocalS) {

            if (newUserName != prop.userName) {
                let newUser = {
                    userName: newUserName,
                    userPass: newUserPass
                }

                meaningsLocalS.push(newUser);

                let newJson = JSON.stringify(meaningsLocalS);
                localStorage.setItem('user', newJson);

                this.user.setUserLoggedIn();
                this.router.navigate(['/post']);

                break;
            } else {
                alert('Пользователь с таким логином существует.');
                break;
            }
        }



    }

    localStor() {
      // Не понял как записать сразу в локал
      const locSt = [
          {
              userName: "admin",
              userPass: "admin"
          },
          {
              userName: "marusya",
              userPass: "123"
          }
      ];
      let lc = JSON.stringify(locSt);
      localStorage.setItem('user', lc);
    }

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question