Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

97
Vistas
Filter data in NodeJS

I have a json array where it contains an objects having name and rating fields.I want to filter that array where rating is 5.It might be the easy one find but I got stuck. Below is my json object.

let products = [
{
  "name" : "product 1",
  "rating" : 5   
},
{
  "name" : "product 2",
  "rating" : 4
},
{
  "name" : "product 3",
  "rating" : 5
},
{
  "name" : "product 4",
  "rating" : 2
}]

Here I am using filter functionality but unable to get How can I use it properly.

This is the function I have written but its not working.

const prod = (products) => {

    products.filter((proct) => {
        if(proct === 5){
          console.log(proct.name);
        }
    });
}

prod(products);

Someone let me know what I am doing wrong.

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

filter function must return a boolean as result

Function is a predicate, to test each element of the array. Return a value that coerces to true to keep the element, or to false otherwise.

Array.prototype.filter

let products = [{ "name": "product 1", "rating": 5 }, { "name": "product 2", "rating": 4 }, { "name": "product 3", "rating": 5 }, { "name": "product 4", "rating": 2 } ]

products = products.filter(({rating}) => rating === 5)

console.log(products)

Each item in your array is an object, and I guess you are looking for rating===5.

Your function should be:

let products = [{ "name": "product 1", "rating": 5 }, { "name": "product 2", "rating": 4 }, { "name": "product 3", "rating": 5 }, { "name": "product 4", "rating": 2 } ]

const prod = (products) => {
  products.filter((proct) => {
    if (proct.rating === 5) {
      console.log(proct.name);
      return true;
    }
    return false
  });
}

prod(products)


Getting just name key:

Take a look at map function which create a new array from old array:

.map(({name}) => name)

Full code:

let products = [{ "name": "product 1", "rating": 5 }, { "name": "product 2", "rating": 4 }, { "name": "product 3", "rating": 5 }, { "name": "product 4", "rating": 2 } ]

products = products.filter(({rating}) => rating === 5).map(({name}) => name)

console.log(products)

about 4 years ago · Juan Pablo Isaza Denunciar

0

Your code is not checking the property. But the object itself. Use the . operator to check the specific property:

let products = [
{
  "name" : "product 1",
  "rating" : 5   
},
{
  "name" : "product 2",
  "rating" : 4
},
{
  "name" : "product 3",
  "rating" : 5
},
{
  "name" : "product 4",
  "rating" : 2
}]

const prod = (products) => {

    products.filter((proct) => {
        if(proct.rating === 5){
          console.log(proct.name);
        }
    });
}

prod(products);

about 4 years ago · Juan Pablo Isaza Denunciar

0

Your condition is comparing proct, an object, to the value 5. You probably forgot to access the rating property of proct:

products.filter((proct) => {
    if(proct.rating === 5){
      console.log(proct.name);
    }
});

However, you are using filter wrong. Filter is a method whose entire point is to get all the elements in the array that answer a condition and return them, and not just iterate over an array and execute code (that's what forEach is for). A more correct code would be:

const filteredProducts = products.filter((proct) => proct.rating === 5);
console.log(filteredProducts); // [{ name: "product 1", rating: 5 }, { name: "product 3", rating: 5 }]

filteredProducts.forEach((proct) => console.log(proct.name));

This solution first retrieves all the products whose rating is 5 and then iterates over each of them to print their name.

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda