const menu_type = menus.filter(menu => menu.type);
the JSON has a type key that is not a ‘Show All’ value. now I filter the menus menus.type !== Show All
Method 1
const [filteredMenus, setFilteredMenus] =useState([]);
const getFilteredMenus = () => {
if (activeTab === 'Show All') {
setFilteredMenus(menus)
}
setFilteredMenus(menus.filter(menu => menu.type === tabs[0]))
}
const filteredMenu = getFilteredMenus();
Method 2
const menu_type = ['Show All','Beverages','Lunch','Dinner'];
const menus_1 = menus.filter(menu => menu.type === menu_type[1]);
const menus_2 = menus.filter(menu => menu.type === menu_type[2]);
const menus_3 = menus.filter(menu => menu.type === menu_type[3]);
method 3
const types = ['Beverages', 'Lunch', 'Dinner'];
const filteredMenus = (!types.includes(type)) ? (menus) : (menus.filter(menu => menu.type === type) );
These 3 methods can be used to filter the key. but It uses the map method doesn’t work it
New contributor
Yasiru Viyara is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.