const menuWrapper = document.querySelector('.portfolio-menu'),
menuElems = menuWrapper.querySelectorAll('li'),
allButton = menuWrapper.querySelector('.all'),
loveButton = menuWrapper.querySelector('.lovers'),
chefButton = menuWrapper.querySelector('.chef'),
girlButton = menuWrapper.querySelector('.girl'),
guyButton = menuWrapper.querySelector('.guy'),
grandmotherButton = menuWrapper.querySelector('.grandmother'),
grandfatherButton = menuWrapper.querySelector('.granddad');
const buttons = {};
'all,love,chef,girl,boy,grandma,grandpa'
.split(',').forEach((name) => buttons[name] = menuWrapper.querySelector('.' + name));
Использовать потом, например, вместо girlButton
— buttons.girl
const menuQuerySelector = (selectors) => selectors.map((s) => menuWrapper.querySelector(s));
const buttonSelectors = ['.b1', '.b2', '.b3', '.b4', ...];
const [button1, button2, button3, button4, ...] = menuQuerySelector(buttonSelectors);
let objects = {
allButton: {
wrapper: 'menuWrapper',
selector: 'li',
'function': 'querySelectorAll'
},
loveButton : {
wrapper: 'menuWrapper',
selector: '.lovers',
'function': 'querySelector'
},
};
Object.entries(objects).forEach(entry => {
const [key, value] = entry;
const wrapper = document.querySelector(value.wrapper);
value.object = wrapper[value.function](value.selector);
});
const menuWrapper = document.querySelector(".portfolio-menu");
if (menuWrapper !== null) {
const [all, lovers, chef, girl, guy, grandmother, granddad] = [
".all",
".lovers",
".chef",
".girl",
".guy",
".grandmother",
".granddad",
].map((selector) => {
return menuWrapper.querySelector(selector);
});
const menuElems = menuWrapper.querySelectorAll("li");
}