function collectValues(target, ...sources) {
const onInput = () => target.value = sources.map(n => n.value).join(', ');
sources.forEach(n => n.addEventListener('input', onInput));
return () => sources.forEach(n => n.removeEventListener('input', onInput));
}
collectValues(...document.querySelectorAll('input'));
Object.values(arr.reduce((acc, { a, b }) => (
(acc[a] ??= { a, items: [] }).items.push(b),
acc
), {}))
document.addEventListener('click', ((val, e) => {
console.log(val);
}).bind(null, isOpenInputForSearch));
toggle не срабатывает при клике на элемент. Не присваивает классы и не убирает.
Клик происходит по label
[...arr].sort().reduce((acc, n, i) => (
(i && n.includes(acc[acc.length - 1])) || acc.push(n),
acc
), [])
arr.filter((n, i, a) => !a.some((m, j) => i !== j && n.includes(m)))
if(num=>5&&num<=10){
=>
- никакой это не оператор сравнения.true
. const pick = (obj, keys) => Object.fromEntries(keys.map(k => [ k, obj[k] ]));
const newArr = arr.map(n => pick(n, keys));
const pick = (obj, keys) =>
Object.fromEntries(Object.entries(obj).filter(m => keys.includes(m[0])));
// или
const pick = (obj, keys) =>
keys.reduce((acc, n) => (obj.hasOwnProperty(n) && (acc[n] = obj[n]), acc), {});
a
, показывать пытаетесь img
.document.querySelectorAll('.photos img').forEach(n => {
n.parentNode.style.display = +n.alt === alt ? '' : 'none';
});
function getTimes(start, end, interval) {
const result = [];
const _start = moment(start, 'HH:mm');
const _end = moment(end, 'HH:mm');
if (_start > _end) {
_end.add(1, 'day');
}
while (_start <= _end) {
result.push(_start.format('HH:mm'));
_start.add(interval, 'minutes');
}
return result;
}
const times = getTimes('10:00', '18:00', 30);
const valueAndClass = [
[ 'yes', 'bg-success' ],
[ 'no', 'bg-warning' ],
];
document.querySelector('table').addEventListener('change', e => {
for (const td of e.target.closest('tr').cells) {
for (const [ value, className ] of valueAndClass) {
td.classList.toggle(className, value === e.target.value);
}
}
});
bg-primary
у ячеек, добавьте его строкам, а обработчик события change пусть будет таким, например:document.querySelector('table').addEventListener('change', ({ target: t }) => {
const tr = t.closest('tr');
valueAndClass.forEach(n => tr.classList.toggle(n[1], n[0] === t.value));
});
const $container = $('.promo__cats');
$container.append(...$container
.children()
.get()
.map(n => [ n, +$('.promo__price', n).text().replace(/\D/g, '') ])
.sort((a, b) => a[1] - b[1])
.map(n => n[0])
);
cells.push(xCells); buffCells.push(xCells);
cells = buffCells;
const container = document.querySelector('селектор контейнера с select\'ами');
const selectSelector = 'селектор select\'ов';
const input = document.querySelector('селектор input\'а');
const updateInput = selects => input.value = Array.from(selects, n => n.value).join(' ');
container.addEventListener('change', function() {
updateInput(this.querySelectorAll(selectSelector));
});
// или
const selects = container.querySelectorAll(selectSelector);
const onChange = updateInput.bind(null, selects);
selects.forEach(n => n.addEventListener('change', onChange));
function parallel(tasks, onAllTasksComplete) {
const results = [];
let numCompleted = 0;
function onTaskComplete(index, result) {
results[index] = result;
if (++numCompleted === tasks.length) {
onAllTasksComplete(results);
}
}
for (let i = 0; i < tasks.length; i++) {
const onComplete = r => onTaskComplete(i, r);
const result = tasks[i](onComplete);
if (result !== undefined) {
onComplete(result);
}
}
}
const process = (funcs, initialArg) => funcs.reduce((arg, f) => f(arg), initialArg);
const getMonthNameInGenitiveCase = (date = new Date) =>
date.toLocaleString('ru', {
month: 'long',
day: 'numeric',
}).split(' ')[1];
const getMonthNameInGenitiveCase = (date = new Date) =>
[
'января', 'февраля', 'марта', 'апреля', 'мая', 'июня',
'июля', 'августа', 'сентября', 'октября', 'ноября', 'декабря',
][date.getMonth()];
const openSelector = '[data-target]';
const closeSelector = '.modal__close';
const modalSelector = '.modal';
const activeClass = 'modal--active';
document.addEventListener('click', ({ target: t }) => {
const open = t.closest(openSelector);
if (open) {
document.querySelector(`#${open.dataset.target}`).classList.add(activeClass);
} else {
t.closest(closeSelector)?.closest(modalSelector).classList.remove(activeClass);
}
});
const onClick = (selector, handler) => document
.querySelectorAll(selector)
.forEach(n => n.addEventListener('click', handler));
onClick(openSelector, ({ currentTarget: { dataset: { target } } }) => {
document.querySelector(`#${target}`).classList.add(activeClass);
});
onClick(closeSelector, e => {
e.currentTarget.closest(modalSelector).classList.remove(activeClass);
});
const obj = {
val: 1,
valueOf() {
return this.val ^= 1;
},
};
// или
const obj = {
val: '1',
toString() {
return this.val = '1'.slice(this.val.length);
},
};
// или
const obj = {
val: true,
[Symbol.toPrimitive]() {
return this.val = !this.val;
},
};
console.log(obj == false, obj == true); // true true
console.log(...Array.from({ length: 5 }, () => +obj)); // 0 1 0 1 0
function mustStay(n) {
for (const m of arr2) {
if (m.content.insuranceType === n.value) {
return false;
}
}
return true;
}
// или
const mustStay = n => arr2.every(m => m.content.insuranceType !== n.value);
// или
const mustStay = function(n) {
return !this.has(n.value);
}.bind(new Set(arr2.map(n => n.content.insuranceType)));
const filteredArr1 = arr1.filter(mustStay);
arr1.reduceRight((_, n, i, a) => mustStay(n) || a.splice(i, 1), null);
// или
arr1.splice(0, arr1.length, ...arr1.filter(mustStay));
// или
let numDeleted = 0;
for (const [ i, n ] of arr1.entries()) {
arr1[i - numDeleted] = n;
numDeleted += !mustStay(n);
}
arr1.length -= numDeleted;