I'm facing an issue while filtering a Javascript array of objects with nested objects based on specific properties. I can successfully filter by name, slug, website, and launch year without any problem. However, when it comes to filtering the category name (category.name) which is inside the object, it doesn't seem to work. Why does the filter for category name fail?
var search = "qui"; // doesn't work (category.name)
// var search = "Sauer"; // works (name)
var data = [{ "name": "Sauer-Metz", "slug": "ab-laborum",
"website": "https://test.com", "launch_year": 2017, "category_id": 6,
"category": { "id": 6, "name": "qui", "slug": "qui" } } ];
var results = data.filter(company => [
'name', 'launch_year', 'website', 'category.name'
].some(key => String(company[key]).toLowerCase().includes(search.toLowerCase())));
console.log(results);