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.
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.
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)
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);
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.