const newArr = arr.reduce((acc, n) => (
acc.push({ ...n, fractionTotal: n.fraction + (acc.at(-1)?.fractionTotal ?? 0) }),
acc
), []);
// или
const newArr = arr.map(function({ ...n }) {
n.fractionTotal = this[0] += n.fraction;
return n;
}, [ 0 ]);
arr.forEach((n, i, a) => n.fractionTotal = n.fraction + (i && a[i - 1].fractionTotal));
// или
arr.reduce((acc, n) => n.fractionTotal = acc + n.fraction, 0);
const rows = 8;
const cols = 8;
document.body.innerHTML = Array
.from({ length: rows }, (_, i) => Array
.from({ length: cols }, (_, j) => (i ^ j) & 1)
.join(''))
.join('<br>');
const sorted = (data, keys) => Array
.from(data, n => [ n ].concat(keys(n)))
.sort((a, b) => {
let diff = 0;
for (let i = 0; ++i < a.length && !(diff = ((a[i] < b[i]) ? -1 : +(a[i] > b[i])));) ;
return diff;
})
.map(n => n[0]);
const sortChildren = (el, keys) =>
el.append(...sorted(el.children, keys));
const ul = document.querySelector('ul');
ul.addEventListener('change', e => sortChildren(
e.currentTarget,
el => [
-el.querySelector('input').checked,
el.innerText.trim().toLowerCase(),
]
));
ul.dispatchEvent(new Event('change'));
class Solution:
def rotate(self, matrix: List[List[int]]) -> None:
length = len(matrix)
for i in range(0, length // 2):
for j in range(i, length - i - 1):
matrix[i][j], \
matrix[j][length - i - 1], \
matrix[length - i - 1][length - j - 1], \
matrix[length - j - 1][i] \
= \
matrix[length - j - 1][i], \
matrix[i][j], \
matrix[j][length - i - 1], \
matrix[length - i - 1][length - j - 1]
arr.filter(RegExp.prototype.test.bind(/^[рим]*$/i))
// или
arr.filter(n => !n.match(/[^рим]/i))
// или
arr.filter(n => !n.replace(/р|и|м/gi, ''))
// или
arr.filter(n => [...n.toLowerCase()].every(m => 'рим'.includes(m)))
const result = Object.values(data.reduce((acc, n) => (
(acc[n.brand] ??= { ...n, price: 0 }).price += n.price,
acc
), {}));
function uniqueWithSum(data, key, sumKey) {
const getKey = key instanceof Function ? key : n => n[key];
const unique = new Map;
for (const n of data) {
const k = getKey(n);
unique
.set(k, unique.get(k) ?? (u => (u[sumKey] = 0, u))(structuredClone(n)))
.get(k)[sumKey] += n[sumKey];
}
return [...unique.values()];
}
// ваш случай
const result = uniqueWithSum(data, n => n.brand, 'price');
// элементам исходного массива не обязательно быть объектами
uniqueWithSum([
[ 'aaa', 1 ],
[ 'aaa', 10 ],
[ 'aaa', 100 ],
[ 'bbb', 666 ],
], 0, 1) // [ [ 'aaa', 111 ], [ 'bbb', 666 ] ]
ul
в transition
следует завернуть li
в transition-group
:<transition-group tag="ul" name="fade" class="buttons">
<li
v-if="какое здесь будет условие, предлагаю подумать самостоятельно"
...
const itemSelector = '.radio';
const activeClass = 'active';
// конечно, вы можете и дальше продолжать ковырять jquery
const $items = $(itemSelector).on('change', function() {
$items.next().removeClass(activeClass);
$(this).next().addClass(activeClass);
});
// но ведь есть и другой путь
const items = document.querySelectorAll(itemSelector);
const onChange = ({ currentTarget: t }) =>
items.forEach(n => n.nextElementSibling.classList.toggle(activeClass, n === t));
items.forEach(n => n.addEventListener('change', onChange));
.radio:has(:checked) + .order__form-input {
/* сюда переносим стили, делающие input видимым */
}
arr.reduce((acc, n) => (
Object.entries(n.json_build_object).forEach(([ k, v ]) => {
(acc[k] ??= []).find(m => m.id === v.id) || acc[k].push(v);
}),
acc
), {})
links.reduce((acc, n) => {
const path = n.url.split('/');
const [ lastFolder ] = path.splice(-2);
(path.reduce((p, c) => p[c] ??= {}, acc)[lastFolder] ??= []).push(n);
return acc;
}, {})
links.reduce((acc, n) => (
n.url
.match(/[^\/]+(?=\/)/g)
.reduce((p, c, i, a) => p[c] ??= (-~i < a.length ? {} : []), acc)
.push(n),
acc
), {})
#itemInner {
counter-reset: bullshit-counter;
}
.row {
counter-increment: bullshit-counter;
}
.row::before {
content: counter(bullshit-counter) "!!!";
}
itemInner.find('.item-num').text(i => i + 1);
const parent = document.querySelector('#questions');
const selector = '#point';
const children = [...parent.children];
const index = -~children.findIndex(n => n.matches(selector));
index && parent.replaceChildren(...children.slice(0, index));
// или
for (
const el = parent.querySelector(selector);
el?.nextElementSibling;
el.nextElementSibling.replaceWith()
) ;
// или
parent.querySelectorAll(`${selector} ~ *`).forEach(n => n.remove());
document.querySelector('.shopWrapper').addEventListener('mouseover', function() {
const color = `#${Math.random().toString(16).slice(2, 8).padEnd(6, 0)}`;
this.style.setProperty('--random-color', color);
});