for const [name, button] of Object.entries(buttons) {
button.addEventListener('click', () => show( this[name + 'Items']);
}
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 menuQuerySelector = (selectors) => selectors.map((s) => menuWrapper.querySelector(s));
const buttonSelectors = ['.b1', '.b2', '.b3', '.b4', ...];
const [button1, button2, button3, button4, ...] = menuQuerySelector(buttonSelectors);
const buttons = {};
'all,love,chef,girl,boy,grandma,grandpa'
.split(',').forEach((name) => buttons[name] = menuWrapper.querySelector('.' + name));
Использовать потом, например, вместо girlButton
— buttons.girl