Search code examples
javascriptarraystypescriptarray-filter

JSON Filter return null


I´m trying to filter JSON array in Angular like this

JSON

1 46 3 2 48 4

FILTER

   var test = this.users.filter(element => {
            element.id === 1;
          });

The problem is 'test' variable return null in every cases. As we can see the 'id' 1 exist, so theoretically should return data. ¿Anybody know what is wrong in this code?

UPDATE

Full JSON response

  {id: 1, usuario: "alara", nombre: "alara", password: null, apellidos: "alara", …}
  {id: 46, usuario: "ale", nombre: "ale", password: null, apellidos: "ale", …}
  {id: 3, usuario: "jorge", nombre: "jorge", password: null, apellidos: null, …}
  {id: 2, usuario: "apple", nombre: "apple", password: null, apellidos: null, …}
  {id: 48, usuario: "arsontech", nombre: "arsontech", password: null, apellidos: null, …}
  {id: 47, usuario: "Pedro", nombre: "pedro", password: null, apellidos: "Bilbao", …}

Solution

  • var test = this.users.filter(element => {
            element.id === 1;
          });
    

    You don't return anything.

    var test = this.users.filter(element => element.id === 1);
    

    Should work better. Or you can also use this :

    var test = this.users.filter(element => {
      return element.id === 1;
    });