function chunked(arr, numChunks) {
const chunks = [];
const chunkSize = arr.length / numChunks | 0;
const numLooseItems = arr.length % numChunks;
for (let i = 0, j = 0; j < numChunks; j++) {
chunks.push(arr.slice(i, i += chunkSize + (j < numLooseItems)));
}
return chunks;
}
const [ head, tail ] = chunked(arr, 2);
const itemSelector = '.parent';
const className = 'xxx';
document.querySelectorAll(itemSelector).forEach(n => {
n.addEventListener('mouseenter', onHover);
n.addEventListener('mouseleave', onHover);
});
function onHover(e) {
const state = e.type === 'mouseenter';
for (
let el = this;
(el = el.nextElementSibling) && !el.matches(itemSelector);
el.classList.toggle(className, state)
) ;
}
.parent
, чтобы создать видимость, будто бы стили не применялись:.parent:hover ~ .child {
...
}
.parent:hover ~ .parent ~ .child {
...
}
document.querySelectorAll('.selectit > input').forEach(n => {
n.checked = genres.includes(n.parentNode.innerText.trim());
});
for (const n of document.getElementsByClassName('selectit')) {
n.children[0].checked = genres.indexOf(n.textContent.trim()) > -1;
}
ind.map(((data, n) => data[n]).bind(null, arr.reduce((acc, row) => {
const d = acc[row[0]];
row.forEach((n, i) => i && (d[i] = d[i] || []).push(n));
return acc;
}, ind.reduce((acc, n) => (acc[n] = [ n ], acc), {}))))
array.reduce((acc, el) => {
el.forEach((n, i) => {
acc[i] = acc[i] || [];
n.forEach((m, j) => (acc[i][j] = acc[i][j] || []).push(...[].concat(m)));
});
return acc;
}, [])
const result = ind.reduce((acc, col) => {
arr.forEach((n, i) => {
acc[i].push(i ? n.filter((m, j) => arr[0][j] === col) : col);
});
return acc;
}, arr.map(() => []));
const indObj = Object.fromEntries(ind.map((n, i) => [ n, i ]));
const result = arr.reduce((acc, n, i) => (
i && n.forEach((m, j) => acc[i][indObj[arr[0][j]]].push(m)),
acc
), arr.map((_, i) => ind.map(n => i ? [] : n)));
const className = 'qq-upload-file';
.const elements = document.querySelectorAll(`.${className}`);
// или
const elements = document.getElementsByClassName(className);
const getText = el => el.textContent;
// или
const getText = el => el.innerText;
// или (т.к., вложенных элементов нет)
const getText = el => el.innerHTML;
const texts = Array.from(elements, getText);
// или
const texts = Array.prototype.map.call(elements, getText);
// или
const texts = [];
for (const n of elements) {
texts.push(getText(n));
}
// или
const texts = [];
for (let i = 0; i < elements.length; i++) {
texts[i] = getText(elements[i]);
}
const uniqueCount = new Set(texts).size;
// или
const { size: uniqueCount } = new Map(texts.map(n => [ n, n ]));
// или
const [ uniqueCount ] = texts.reduce((acc, n) => {
acc[0] += !(acc[1][n] = acc[1].hasOwnProperty(n));
return acc;
}, [ 0, {} ]);
// или
const uniqueCount = texts.filter((n, i, a) => i === a.indexOf(n)).length;
const inputs = document.querySelectorAll('form input');
// или
const inputs = document.forms[0].elements;
// или
const inputs = document.querySelector('form').getElementsByTagName('input');
const getName = el => el.name;
// или
const getName = el => el.getAttribute('name');
// или
const getName = el => el.attributes.name.value;
const names = Array.from(inputs, getName);
// или
const names = [].map.call(inputs, getName);
// или
const names = [];
for (const n of inputs) {
names.push(getName(n));
}
// или
const names = [];
for (let i = 0; i < inputs.length; i++) {
names[i] = getName(inputs[i]);
}
const result = Object
.entries(arr.reduce((acc, n, i) => ((acc[n] = acc[n] || []).push(i), acc), {}))
.map(([ label, indexes ]) => indexes.length > 1 ? { label, indexes } : label);
const result = Object.values(arr.reduce((acc, n) => (
(acc[n.ID] = acc[n.ID] || { ID: n.ID })[n.FIELD] = n.VALUE,
acc
), {}));
setInterval(count => {
const
len = count.length,
val = `${+count.map(n => n.textContent).join('') + 1}`.padStart(len, 0).slice(-len);
count.forEach((n, i) => n.textContent = val[i]);
}, 50, [...document.querySelectorAll('.count')]);
document.querySelectorAll('div[name] ul[name]').forEach(n => {
if (n.getAttribute('name') !== n.closest('div[name]').getAttribute('name')) {
n.remove();
}
});
for (const n of document.querySelectorAll('div[name]')) {
for (const m of n.querySelectorAll(`ul:not([name="${n.attributes.name.value}"])`)) {
m.parentNode.removeChild(m);
}
}
const sorted = (arr, key) => arr
.map(n => [ n, key(n) ])
.sort((a, b) => a[1] - b[1])
.map(n => n[0]);
// если элементы с отсутствующим номером квартиры должны оказаться
// в начале, а не в конце, то вместо Infinity надо поставить 0
const sortedArr = sorted(arr, n => +(n.address.match(/кв.\s*(\d+)/) || [ Infinity ]).pop());