Basic comparison - Input and Object

0

I need to make a comparison (if) between the value of an input with 2 properties of an object and if they match, enter the system.

function datosPersonal(usuario, correo, dni, contraseña) {
    //Objeto Personal
    this.usuario = usuario;
    this.correo = correo;
    this.dni = dni;
    this.contraseña = contraseña;
  }

    var lauty = new datosPersonal('lauty', '[email protected]', '23232323', '123321');
    var danielitax = new datosPersonal('danielitax', '[email protected]', '41111111', 'daniela123');


$('#goLogin').click(function(){
    var user = $('#inputUser').val();
    var pass = $('#inputPassword').val();
});

I did the one if like this:

$('#goLogin').click(function(){
    var user = $('#inputUser').val();
    var pass = $('#inputPassword').val();

    if(user == user.usuario && pass == user.contraseña) {
        alert('Logeado');
    } else {
        alert('Los datos no coinciden.')
    }
});
  

In the if, when you put user.username user.password the variable user, I want you to take it, for the value entered in the input. That is my problem!

    
asked by lautyyyyyy 02.09.2017 в 23:15
source

2 answers

0

Here I managed to do it with Object.getOwnPropertyDescriptor

  

Like this:

var userLocal = $("[name='inputUser']").val();
    var pass = $("[name='inputPassword']").val();
    var datoUsuario = Object.getOwnPropertyDescriptor(window[userLocal] , 'usuario').value;
    var datoContraseña = Object.getOwnPropertyDescriptor(window[userLocal], 'contraseña').value;

    if(userLocal == datoUsuario && pass == datoContraseña) {
        alert('Logeado');
    } else {
        alert('Los datos no coinciden.')
    }
    
answered by 04.09.2017 / 00:43
source
0

You have to rename the variable user to another name because priority will always be given to local objects that have the same name as a more global object.

For example:

var usuario = { nombre: "einer" };

function imprimirNombreUsuario(parametro)
{
  var usuario = parametro.toUpperCase();
  console.log(usuario);
  
}

imprimirNombreUsuario("matias");

The local variable usuario has a closer field than the global variable usuario so if we want to access the object with the furthest scope, we have to rename the local variable and it's done:

var usuario = { nombre: "einer" };

function imprimirNombreUsuario(parametro)
{
  var usuario_local = parametro.toUpperCase();
  console.log(usuario_local);
  console.log(usuario.nombre);
}

imprimirNombreUsuario("matias");

In your case it would be simply renaming the local variable user to any other name:

$('#goLogin').click(function(){
    var userLocal = $('#inputUser').val();
    var pass = $('#inputPassword').val();

    if(userLocal == user.usuario && pass == user.contraseña) {
        alert('Logeado');
    } else {
        alert('Los datos no coinciden.')
    }
});
    
answered by 03.09.2017 в 04:41