Click here to Skip to main content
15,940,246 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
I have created a class named restaurant manager with an array named as 'restaurantList' . Inside this array I have passed name of restaurant with their address and city . After that I have created a function named as 'filterRestaurantByCity' in which I am trying to filter the restaurant name on the basis of the city provided in the argument..
THIS MY JAVASCRIPT CODE:-
JavaScript
<pre>class restaurantManager {
    constructor() {
        this.restaurantList = [
           { name: "Barbeque Nation", address: "varanasi", city: "varansi" },
            { name: "The Table", address: "varnasi", city: "varansi" },
            { name: "i love my wife", address: "varansi", city: "varansi" },
            { name: "Batti chokha", address: "varansi", city: "varansi" }
        ]
    }
filterRestaurantByCity(varansi){
        let givencity = varansi
        let b = this.restaurantList.filter((city) => {return city==givencity})
        console.log(b.name)
    }
}
    let myobject = new restaurantManager()
   
    let c = myobject.filterRestaurantByCity()

Getting undefined in the console ....

What I have tried:

CONFUSED, WHAT TO DO??
filterRestaurantByCity(varansi){

       let b = this.restaurantList.filter(item =>(item.city==varansi))
       console.log(b.name)
   }

Getting undefined after executing this one....

 filterRestaurantByCity(varansi){
        let givencity = varansi
        let b = this.restaurantList.filter((item) => {return item.city==givencity})
        console.log(b.name)
    }
}


Getting undefined after this too.....
Posted
Updated 9-Sep-22 0:23am
v3
Comments
Richard Deeming 9-Sep-22 4:34am    
REPOST
This is exactly the same question you posted yesterday, which you said you had solved.
How do I solve the printallrestaurantbyname is not defined error at htmlbuttonelement.onclick”?[^]

The solution has not changed in the last 24 hours.
Ujala 2022 9-Sep-22 5:17am    
Updated the question now...
Ujala 2022 9-Sep-22 5:22am    
Solved the previous one
Richard MacCutchan 9-Sep-22 6:26am    
You have not defined the varialble varansi. You need to pass it in on the line:
let c = myobject.filterRestaurantByCity()

1 solution

You've managed to correct the issue where you were comparing the wrong thing:
JavaScript
this.restaurantList.filter((item) => {return item.city==givencity})

This is correct, you should be looking at the city property. Now, if you're saying your console is printing undefined when you open your page then you need to take a look at your script:
JavaScript
let c = myobject.filterRestaurantByCity()

Your filterRestaurantByCity method has a parameter named varansi, but on this line you're not passing in a value. How do you expect the JS to filter by city if you're not providing a city name?
 
Share this answer
 
Comments
Ujala 2022 10-Sep-22 1:02am    
How to pass the value?
Chris Copeland 11-Sep-22 14:42pm    
Passing values to a function is one of the most basic concepts of programming. Here's a good page providing an example[^] which you can use.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900