const values = [ 'text', 'move', 'lols' ];
const selector = values.map(n => `[data-cmd="${n}"]`).join(', ');
const elements = document.querySelectorAll(selector);const hide = el => el.style.display = 'none';
// или
const hide = el => el.style.setProperty('visibility', 'hidden');
// или
const hide = el => el.style.cssText += 'opacity: 0';
// или
const hide = el => el.setAttribute('style', 'transform: scale(0)');
// или
const hide = el => el.hidden = true;elements.forEach(hide);
// или
for (const n of elements) {
hide(n);
}
// или
for (let i = 0; i < elements.length; i++) {
hide(elements[i]);
}
// или
(function next(i, n = elements.item(i)) {
n && (hide(n), next(-~i));
})(0);
$('.count').on('input', e => $('.game_iframe').width(e.target.value));document.querySelector('.count').addEventListener('input', function() {
document.querySelector('.game_iframe').style.width = `${this.value}px`;
});
function createRandomArr(size, min, max) {
const arr = Array.from({ length: max - min + 1 }, (_, i) => i + min);
for (let i = arr.length; --i > 0;) {
const j = Math.random() * -~i | 0;
[ arr[j], arr[i] ] = [ arr[i], arr[j] ];
}
return arr.slice(-size);
}
const arr = createRandomArr(4, 1, 9);
const containerSelector = '.dog';
const buttonSelector = 'i.fa-dog';
const counterSelector = '.score_counter';$(buttonSelector).click(() => {
$(counterSelector).text((i, val) => +val + 1);
});
// или
document.querySelector(buttonSelector).addEventListener('click', () => {
document.querySelector(counterSelector).textContent -= -1;
});.dog несколько:$(containerSelector).on('click', buttonSelector, e => {
$(counterSelector, e.delegateTarget).text((i, val) => -~val);
});
// или
document.querySelectorAll(buttonSelector).forEach(function(n) {
n.addEventListener('click', this);
}, ({ currentTarget: t }) => t
.closest(containerSelector)
.querySelector(counterSelector)
.innerText++
);
const $thumbs = $('.thumbs');
$('.show-image').click(function(e) {
e.preventDefault();
$('.images img.active').removeClass('active');
$('.images img').eq(this.dataset.index).addClass('active');
}).attr('data-index', i => i);
$('.next').click(() => $thumbs.append($thumbs.find('.show-image').first()));
$('.prev').click(() => $thumbs.prepend($thumbs.find('.show-image').last()));
const vowelCount = str => str.replace(/[^aeiouy]/gi, '').length;const vowelCount = str => ~-str.split(/[aeiouy]/i).length;const vowelCount = str =>
Array.prototype.reduce.call(
str.toLowerCase(),
(acc, n) => acc + 'aeiouy'.includes(n),
0
);const vowelCount = str =>
eval(Array.from(str, n => +!!~'aeiouyAEIOUY'.indexOf(n)).join('+')) || 0;
<button>toggle disabled</button>
<select>
<option value="">выбрать</option>
<option value="69">hello, world!!</option>
<option value="187">fuck the world</option>
<option value="666">fuck everything</option>
</select>const $select = $('select');
$('button').click(function() {
$select
.prop('disabled', (i, val) => !val)
.val('')
.find('[value=""]')
.text(() => $select.prop('disabled') ? 'тут ничего нет' : 'выбрать');
});
// или
const select = document.querySelector('select');
document.querySelector('button').addEventListener('click', () => {
select.disabled = !select.disabled;
select.value = '';
select[0].textContent = select.disabled ? 'тут ничего нет' : 'выбрать';
});
const elems = document.querySelectorAll('.hint');
// или
const elems = document.getElementsByClassName('hint');const getText = el => el.textContent;
// или
const getText = el => el.innerText;
// или
const getText = el => el.innerHTML;
// или
const getText = el => el.lastChild.nodeValue;
// или
const getText = el => el.childNodes[0].data;const hints = Array.from(elems, getText);
// или
const hints = Array.prototype.map.call(elems, getText);
// или
const hints = [];
for (const n of elems) {
hints.push(getText(n));
}
// или
const hints = [];
for (let i = 0; i < elems.length; i++) {
hints[i] = getText(elems[i]);
}
// или
const hints = (function get(i, n = elems.item(i)) {
return n ? [ getText(n), ...get(i + 1) ] : [];
})(0);
const filterSelector = '.s-mobile-filter-type-select';
const selectSelector = '.s-mobile-filter-type-select__select';
const selectedSelector = '.s-mobile-filter-type-select__selected-options-list-content';$(selectSelector).on('change', function() {
$(this)
.closest(filterSelector)
.find(selectedSelector)
.text($('option:selected', this).get().map(n => $(n).text()).join(', '));
});const updateSelected = select => select
.closest(filterSelector)
.querySelector(selectedSelector)
.textContent = Array
.from(select.selectedOptions, n => n.text)
.join(', ');
// можно назначить обработчик события каждому select'у индивидуально
document.querySelectorAll(selectSelector).forEach(function(n) {
n.addEventListener('change', this);
}, e => updateSelected(e.target));
// или, применяем делегирование
document.addEventListener('change', ({ target: t }) => {
if (t.matches(selectSelector)) {
updateSelected(t);
}
});
function uniqueWithSum(arr, key, sumKey) {
const getKey = key instanceof Function ? key : n => n[key];
return Object.values(arr.reduce((acc, n) => {
const k = getKey(n);
acc[k] = acc[k] || Object.assign(n.constructor(), n, { [sumKey]: 0 });
acc[k][sumKey] += n[sumKey];
return acc;
}, {}));
}// ваш случай
const result = uniqueWithSum(arr, n => n.id, 'duration');
// элементам не обязательно быть объектами, это могут быть и массивы
uniqueWithSum([
[ 'A', 1 ],
[ 'B', 5 ],
[ 'A', 2 ],
[ 'A', 3 ],
], 0, 1) // [ [ 'A', 6 ], [ 'B', 5 ] ]
function randomArray(length, min, max) {
max = max || 0;
if (max < min) {
[ min, max ] = [ max, min ];
}
return Array.from(
{ length },
() => min + (Math.random() * (max - min + 1) | 0)
);
}// создаём массив из 10 элементов, значения принадлежат интервалу [ 5, 25 ]
const arr1 = randomArray(10, 5, 25);
// аналогично - минимум и максимум можно указывать в любом порядке
const arr2 = randomArray(10, 25, 5);
// значения принадлежат интервалу [ 0, 25 ]
const arr3 = randomArray(10, 25);
// значения принадлежат интервалу [ -7, 0 ]
const arr4 = randomArray(10, -7);
const capitalize = str => str.replace(/(^|\s|-)+\S/g, m => m.toUpperCase());
$input.on('input', function() {
this.value = Math.min(MAX_VALUE, Math.max(MIN_VALUE, this.value));
});
document.addEventListener('keydown', e => {
if (e.key === 'Escape') {
const popups = document.querySelectorAll('#popupClient, #popupPartner');
popups.forEach(n => n.classList.remove('popup--open'));
}
});