const getNextDay = (day, date = new Date()) =>
new Date(
date.getFullYear(),
date.getMonth(),
date.getDate() + ((7 + day - date.getDay()) % 7)
);
const nextSaturday = getNextDay(6);
const values = Object.values(arr.reduce((acc, n) => {
const t = acc[n.from];
acc[n.from] = (t && t.to > n.to) ? t : n;
return acc;
}, {})).map(n => n.val);const values = Object.values(arr.reduce((acc, n) => {
(acc[n.from] = acc[n.from] || [])[n.to] = n.val;
return acc;
}, {})).map(n => n.pop());const values = [...arr]
.sort((a, b) => (a.from - b.from) || (a.to - b.to))
.filter((n, i, a) => !a[i + 1] || a[i + 1].from !== n.from)
.map(n => n.val);Можно с помощью lodash.
const values = _
.chain(arr)
.groupBy('from')
.map(n => _.maxBy(n, m => m.to).val)
.value();
$(document).on('input', '.price-item', function() {
const min = +$('.min-price').val() || 0;
const max = +$('.max-price').val() || Infinity;
$('.filter')
.hide()
.filter(function() {
const val = parseFloat($('.price', this).text());
return min <= val && val <= max;
})
.show();
});
$('div.award a').each((i, n) => n.onmouseover = n.onmouseout = null);$('div.award a').removeAttr('onmouseover onmouseout');$('div.award a').prop({
onmouseover: null,
onmouseout: null,
});
$('table td:nth-child(1)').html((i, html) => {
return '<a href="?name=' + html + '">' + html + '</a>';
});document.querySelectorAll('tbody tr').forEach(n => {
const td = n.children[0];
const text = td.textContent;
td.innerHTML = `<a href="?name=${text}">${text}</a>`;
});for (const { cells: [ td ] } of document.querySelector('tbody').rows) {
const a = document.createElement('a');
a.href = '?name='.concat(td.innerText);
a.append(...td.childNodes);
td.append(a);
}
const container = document.querySelector('div');
const exclude = [ 'class1', 'class2', 'class3' ];
const process = el => el.style.display = 'none';Array
.from(container.children)
.filter(n => !exclude.some(m => n.classList.contains(m)))
.forEach(process);container
.querySelectorAll(`:scope > :not(${exclude.map(n => `.${n}`)})`)
.forEach(process);
const $select = $('select').change(function() {
const selected = $(':selected', this).get().map(n => n.value);
const disabled = $(':disabled', this).get().map(n => n.value);
$select
.not(this)
.find('option')
.prop('disabled', function(i, val) {
return disabled.includes(this.value) ? val : selected.includes(this.value);
});
});const selects = [...document.querySelectorAll('select')];
selects.forEach(n => n.addEventListener('change', onChange));
function onChange() {
const options = [...this];
const selected = options.filter(n => n.selected).map(n => n.value);
const disabled = options.filter(n => n.disabled).map(n => n.value);
for (const n of selects.flatMap(m => m === this ? [] : [...m])) {
n.disabled = disabled.includes(n.value) ? n.disabled : selected.includes(n.value);
}
}
class="subs", например.$('select').change(function() {
$('.subs [class*="sub"]')
.addClass('hidden')
.filter(`.sub${$(this).val()}`)
.removeClass('hidden');
});
// или
$('select').change(function(e) {
const index = ($(e.target).prop('selectedIndex') || Infinity) - 1;
this.addClass('hidden');
this.eq(index).removeClass('hidden');
}.bind($('.subs').children()));document.querySelector('select').addEventListener('change', e => {
const cls = `sub${e.target.value}`;
document.querySelectorAll('.subs [class*="sub"]').forEach(n => {
n.classList.toggle('hidden', !n.classList.contains(cls));
});
});
// или
document.querySelector('select').addEventListener('change', e => {
const index = ~-e.target.selectedIndex;
Array.prototype.forEach.call(
document.querySelector('.subs').children,
(n, i) => n.classList.toggle('hidden', i !== index)
);
});
const input = document.querySelector('input');
const num = 100;input.addEventListener('change', function() {
this.value = (this.value / num | 0) * num;
});<button data-step="-1">-</button>
<button data-step="+1">+</button>document.querySelectorAll('[data-step]').forEach(function(n) {
n.addEventListener('click', this);
}, e => input.value = +input.value + e.target.dataset.step * num);
const inputs = document.querySelectorAll('ol input');const classes = [...new Set(Array.prototype.flatMap.call(
inputs,
n => [...n.classList]
))];const classes = Array
.from(inputs, n => Array.from(n.classList))
.flat();
// или
const classes = [].concat.apply(
[],
[].map.call(inputs, n => n.className.split(' '))
);
// или
const classes = [];
for (const n of inputs) {
for (const m of n.classList) {
classes.push(m);
}
}
// или
const classes = [];
for (let i = 0; i < inputs.length; i++) {
for (let j = 0; j < inputs[i].classList.length; j++) {
classes[classes.length] = inputs[i].classList[j];
}
}
// или
const getClasses = (classList, i, n = classList.item(i)) =>
n ? [ n, ...getClasses(classList, -~i) ] : [];
const classes = (function get(i, n = inputs.item(i)) {
return n ? [ ...getClasses(n.classList, 0), ...get(++i) ] : [];
})(0);
class deferred {
constructor() {
this.promise = new Promise(resolve => this.resolve = resolve);
}
then(f) {
this.promise = this.promise.then(f);
}
}class deferred {
constructor() {
this.callbacks = [];
}
then(f) {
this.callbacks.push(f);
}
resolve(val) {
this.callbacks.reduce((res, f) => f(res), val);
}
}
Не получается обратиться к элементу по его id.
const svgDocument = document.querySelector('object').contentDocument;
const element = svgDocument.querySelector('здесь указываете нужный id или что там вам надо');