Company logo
  • Empleos
  • Bootcamp
  • Acerca de nosotros
  • Para profesionales
    • Inicio
    • Empleos
    • Cursos y retos
    • Preguntas
    • Profesores
    • Bootcamp
  • Para empresas
    • Inicio
    • Nuestro proceso
    • Planes
    • Pruebas
    • Nómina
    • Blog
    • Comercial
    • Calculadora

0

66
Vistas
.filter return empty array but same condition work with .map

I'm doing a simple search bar on react native

I've got some dummy NftDatas that look like this :

[
  { name: "Lorem ipsum", ... },
  { name: "dolor sit amet", ... },
  { name: "consectetur adipiscing", ... },
  { name: "elementum dolor", ... },
  { name: "sapien eu porttitor", ... },
]

I've got a handleSearch() function which compare the user input to the names in the list :

const handleSearch = (value) => {
    const filteredData = NFTData.filter((item) => {
      item.name.toLowerCase().includes(value.toLowerCase());
    });

    console.log(filteredData);
};

//Return an empty array no matter what value the user inputted

But if I map the array and verify the exact same condition it works perfectly fine :

const handleSearch = (value) => {
    let filteredData2 = [];

    NFTData.map((item) => {
      if (item.name.toLowerCase().includes(value.toLowerCase())) {
        filteredData2.push(item.name);
      }
    });

    console.log(filteredData2);
};

// For user input "lor" return ["Lorem ipsum", "dolor sit amet", etc ...]

Even if my app now works fine I wonder what I did wrong with my .filter function

7 months ago · Juan Pablo Isaza
Responde la pregunta
Encuentra empleos remotos