const sumDiagonals = matrix =>
matrix.reduce((acc, n, i) => {
acc.principal += n[i];
acc.secondary += n[n.length - i - 1];
return acc;
}, {
principal: 0,
secondary: 0,
});
numbers.filter(n => !objects.some(m => m.number === n))
numbers.filter(function(n) {
return !this.has(n);
}, new Set(objects.map(n => n.number)))
numbers.filter(((nums, n) => !nums.includes(n)).bind(null, objects.map(n => n.number)))
Object.values(objects.reduce(
(acc, n) => (delete acc[n.number], acc),
numbers.reduce((acc, n) => (acc[n] = n, acc), {})
))
Array.from(objects.reduce(
(acc, n) => (acc.delete(n.number), acc),
new Map(numbers.map(n => [ n, n ]))
).values())
const newArr = [...arr]
.sort((a, b) => a.name.localeCompare(b.name) || (a.price - b.price))
.filter((n, i, a) => n.name === a[i - 1]?.name);
const newArr = [...arr]
.sort((a, b) => a.name.localeCompare(b.name) || (a.price - b.price))
.filter((n, i, a) => n.name === a[i - 1]?.name || n.name !== a[i + 1]?.name);
const newArr = [...arr]
.sort((a, b) => a.price - b.price)
.filter((n, i, a) => n !== a.find(m => m.name === n.name));
const newArr = [...arr].sort((a, b) => a.price - b.price);
Object
.values(newArr.reduce((acc, n) => ((acc[n.name] ??= []).push(n), acc), {}))
.forEach(n => n.length !== 1 && newArr.splice(newArr.indexOf(n[0]), 1));
const mustStay = n => n !== null;
.const newArr = arr.map(n => ({
...n,
array2: n.array2.filter(mustStay),
}));
arr.forEach(n => n.array2.reduceRight((_, n, i, a) => mustStay(n) || a.splice(i, 1), 0));
// или
for (let i = 0; i < arr.length; i++) {
const a = arr[i].array2;
a.splice(0, a.length, ...a.filter(mustStay));
}
// или
for (const { array2: a } of arr) {
let numDeleted = 0;
for (const [ i, n ] of a.entries()) {
a[i - numDeleted] = n;
numDeleted += !mustStay(n);
}
a.length -= numDeleted;
}
const columnIndex = {
color: 0,
fruit: 1,
};
function filter() {
const filters = Object.entries(Array
.from(document.querySelectorAll('.list-group :checked'))
.reduce((acc, n) => ((acc[n.name] ??= []).push(n.value), acc), {})
);
document.querySelectorAll('table tbody tr').forEach(n => {
n.style.display = filters.some(([ k, v ]) => !v.includes(n.cells[columnIndex[k]].innerText))
? 'none'
: 'block';
});
}
document.querySelectorAll('.list-group').forEach(n => n.addEventListener('change', filter));
function getDays(year, month) {
const days = [];
const d = new Date(year, month, 1);
let week = 1;
while (d.getMonth() === month) {
const date = d.getDate();
const day = d.getDay();
days.push({
day: date,
weeknumber: (day === 1 || date === 1) ? week : false,
weekday: d.toLocaleString('en-US', { weekday: 'short' }),
});
d.setDate(date + 1);
week += !day;
}
return days;
}
text.match(RegExp(str, 'g'))?.length ?? 0
text.split(str).length - 1
const flatObj = obj =>
Object.entries(obj).reduce((acc, [ k, v ]) => (
v instanceof Object && !Array.isArray(v)
? Object.assign(acc, flatObj(v))
: acc[k] = v,
acc
), {});
document.querySelectorAll('h2').forEach(n => {
const [ season, , episode ] = n.innerText.split(' ');
if (+episode === 1) {
n.id = `season-${season}`;
}
});
[...document.querySelectorAll('h2')]
.filter(n => n.innerText.endsWith(', 1 серия'))
.forEach((n, i) => n.id = `season-${i + 1}`);
document.querySelectorAll('h2').forEach((n, i, a) => {
const prev = i && a[i - 1].innerText.match(/\d+/)[0];
const curr = n.innerText.match(/\d+/)[0];
if (curr !== prev) {
n.id = `season-${curr}`;
}
});
Array
.from(document.querySelectorAll('h2'))
.reduce((acc, n) => (acc[parseInt(n.innerText)] ??= n, acc), [])
.forEach((n, i) => n.id = `season-${i}`);
const groupAdjacent = (arr, newGroup) =>
arr.reduce((acc, n, i, a) => (
(!i || newGroup(n, a[~-i])) && acc.push([]),
acc[~-acc.length].push(n),
acc
), []);
const result = groupAdjacent(arr, (c, p) => c !== -~p);
function group(data, key) {
const grouped = new Map;
let i = -1;
for (const n of data) {
const k = key(n, ++i);
grouped.set(k, grouped.get(k) ?? []).get(k).push(n);
}
return grouped;
}
const result = [...group(arr, (n, i) => n - i).values()];
$('.js-cropped-word').text((i, text) => text.replace(/(?<=\S{19,}).+/, '...'));
В Сафари не работает
document.querySelectorAll('.js-cropped-word').forEach(n => {
n.textContent = n.textContent.replace(/(\S{19}).+/, '$1...');
});
const max = 19;
for (const n of document.getElementsByClassName('js-cropped-word')) {
const words = n.innerText.split(' ');
const i = words.findIndex(n => n.length > max);
if (i !== -1) {
words.length = i + 1;
words[i] = words[i].slice(0, max) + '...';
n.innerText = words.join(' ');
}
}
const values = [ '1', '2', '3' ];
[...selectEl].forEach(n => values.includes(n.value) && n.remove());
for (const n of selectEl.querySelectorAll(values.map(n => `[value="${n}"]`))) {
selectEl.removeChild(n);
}
for (let i = selectEl.options.length; i--;) {
const n = selectEl.options[i];
if (values.indexOf(n.value) !== -1) {
n.outerHTML = '';
}
}
calc__price
и calc__payment
заменить на общий calc
.const $price = $('#price');
const $firstPayment = $('#first-payment');
function update($elem) {
$elem.closest('.calc').find('output').text($elem.val());
$('.payment-percent').html(Math.round($firstPayment.val() * 100 / $price.val()) + '%');
}
$price.closest('.calc').on('input', function() {
const val = $price.val();
$firstPayment
.val((i, v) => Math.min(v, val))
.attr('max', val)
.rangeslider('update', true);
update($firstPayment);
});
$price.add($firstPayment)
.rangeslider({ polyfill: false })
.closest('.calc')
.on('input', e => update($(e.target)))
.end()
.trigger('input');
function walkNested(val, callback) {
callback(val);
if (val instanceof Object) {
Object.values(val).forEach(n => walkNested(n, callback));
}
}
walkNested(obj, n => n?.touched === true && (n.touched = false));
function walkNested(val, callback) {
for (const stack = [ val ]; stack.length;) {
const n = stack.pop();
callback(n);
stack.push(...Object.values(n ?? {}));
}
}