Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

96
Views
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 answers
Answer question

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 Report

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!