0

I want to filter this array of object by one favoriteCities for example by Paris but it's not working, I wouls like to get an array with the object that contain "Paris" in the favoriteCities, please hep I tried several options

       const favCity= [{
                    name: "shaun",
                    favoriteCities: ["Paris", "London"]
                  },
                  {
                    name: "valerie",
                    favoriteCities: ["Paris", "NewYork"]
                  },
                  {
                    name: "peter",
                    favoriteCities: ["London", "Berlin"]
                  }
                ]
            
            const res = favCity.filter((ele,i)=>{

 return {
   name: ele.name,
   favoriteCities: ele.favoriteCities.filter(element=>
         element === "Paris" ? ele.favoriteCities : [])
        }
});


console.log(res)
2
  • 1
    ele.favoriteCities.includes("Paris")? Commented Jan 6, 2021 at 23:32
  • You haven't supplied code that makes sense. Is that a function returning an Object? }); suggests not. ele.favoriteCities? Where's that? console.log(res)? res is not defined. Commented Jan 6, 2021 at 23:37

1 Answer 1

2
let favCity= [{
                    name: "shaun",
                    favoriteCities: ["Paris", "London"]
                  },
                  {
                    name: "valerie",
                    favoriteCities: ["Paris", "NewYork"]
                  },
                  {
                    name: "peter",
                    favoriteCities: ["London", "Berlin"]
                  }
                ]
            
favCity = favCity.filter(element => element.favoriteCities.includes("Paris"))

console.log(favCity)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.