document.querySelectorAll('.filter__item').forEach(n => {
n.style.display = Array
.from(n.querySelectorAll('.filter__checkgroup-count'))
.some(m => +m.innerText.trim())
? 'block'
: 'none';
});
.hidden {
display: none;
}
for (const n of document.getElementsByClassName('filter__item')) {
n.classList.toggle('hidden', Array.prototype.every.call(
n.getElementsByClassName('filter__checkgroup-count'),
m => Number(m.textContent.trim()) === 0
));
}
function isEqual(a, b) {
const keysA = Object.keys(a);
const keysB = Object.keys(b);
return keysA.length === keysB.length && keysA.every(n => a[n] === b[n]);
}
const result = arr1.concat(arr2.filter(n => arr1.every(m => !isEqual(n, m))));
const unique = function(arr, keys = n => [ n ]) {
const picked = new Map;
return arr.filter((...args) => {
const p = keys(...args).reduce((acc, k) => acc.set(k, acc.get(k) ?? new Map).get(k), picked);
return !p.set(this, p.has(this)).get(this);
});
}.bind(Symbol());
const result = unique([ ...arr1, ...arr2 ], n => [ n.teamId, n.userId ]);
const zip = (...arrs) => arrs[0]?.map((n, i) => arrs.map(m => m[i])) ?? [];
// или
const zip = (...arrs) =>
arrs.reduce((acc, n, i) => (
n.forEach((m, j) => (acc[j] ??= [])[i] = m),
acc
), []);
// или
const zip = (...arrs) => Array.from(
{ length: Math.max(...arrs.map(n => n.length)) },
(n, i) => arrs.map(m => m[i])
);
const arr = zip(arr1, arr2);
. const chunked = (data, chunkSize) =>
data.reduce((acc, n, i) => (
(acc[i / chunkSize | 0] ??= []).push(n),
acc
), []);
// или
const chunked = (data, chunkSize) =>
data.reduce((acc, n, i) => (
(i % chunkSize) || acc.push([]),
acc.at(-1).push(n),
acc
), []);
console.log(chunked([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ], 3));
const chunked = (data, chunkSize, slice = data.slice) =>
Array.from(
{ length: Math.ceil(data.length / chunkSize) },
function(_, i) {
return this(i * chunkSize, (i + 1) * chunkSize);
},
(slice instanceof Function ? slice : [].slice).bind(data)
);
console.log(chunked([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ], 3));
console.log(chunked(document.querySelectorAll('img'), 5));
console.log(chunked('abcdefghij', 4)); // кусками, кстати, будут тоже строки
console.log(chunked('abcdefghij', 4, [].slice)); // а так - массивы
console.log(chunked($('img'), 5));
function* chunked(data, chunkSize) {
let chunk = [];
for (const n of data) {
if (chunk.push(n) === chunkSize) {
yield chunk;
chunk = [];
}
}
if (chunk.length) {
yield chunk;
}
}
console.log(Array.from(chunked([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ], 3)));
console.log([...chunked(document.querySelectorAll('img'), 5)]);
for (const n of chunked(Array(10).keys(), 4)) {
console.log(n);
}
const sendRequests = (urls, max) =>
new Promise(resolve => {
const results = Array(urls.length).fill(null);
let countRequests = 0;
let countResponses = 0;
for (let i = 0; i < Math.max(1, Math.min(max, urls.length)); i++) {
request();
}
function request() {
if (countResponses === urls.length) {
resolve(results);
} else if (countRequests < urls.length) {
const index = countRequests++;
fetch(urls[index])
.then(result => result.json())
.catch(error => error)
.then(result => {
results[index] = result;
countResponses++;
request();
});
}
}
});
const groupedAndSortedArr = Object
.values(arr.reduce((acc, n) => ((acc[n[0]] ??= []).push(n), acc), {}))
.sort((a, b) => a[0][0].localeCompare(b[0][0]));
str.split('=').pop()
// или
str.slice(str.indexOf('=') + 1)
str.replace(/[^=]*=/, '')
// или
str.match(/(?<==).*/)[0]
// или
/[^=]*$/.exec(str).shift()
new URLSearchParams(str).get('sort')
Array
.from(new URLSearchParams(str))
.reduce((acc, [ k, v ]) => (
k.endsWith('[]')
? (acc[k.slice(0, -2)] ??= []).push(v)
: acc[k] = v,
acc
), {})
function haveSameValues(arr1, arr2) {
if (arr1.length !== arr2.length) {
return false;
}
const count = new Map();
arr1.forEach(n => count.set(n, (count.get(n) ?? 0) + 1));
arr2.forEach(n => count.set(n, (count.get(n) ?? 0) - 1));
return [...count.values()].every(n => n === 0);
}
haveSameValues(
[ 'hello, world!!', 0, 0, 0, 1, 1, false, false ],
[ false, false, 1, 1, 0, 0, 0, 'hello, world!!' ]
) // true
haveSameValues(
[ 1, 2, 3 ],
[ 3, 2, 2 ]
) // false
haveSameValues(
[],
[]
) // true
const arr = Array.from(
new Set(Array.from(document.querySelectorAll('.shop_name'), n => n.innerText)),
n => ({ name: n })
);
const arr = Object.values(Array.prototype.reduce.call(
document.getElementsByClassName('shop_name'),
(acc, { textContent: name }) => (acc[name] ??= { name }, acc),
{}
));
wrapper.innerHTML = Array
.from(word.value, n => obj[n] ? `<img src="${obj[n]}">` : '')
.join('');
function getPaths(obj, path = [ '' ]) {
const entries = Object.entries(obj);
return entries.length
? entries.reduce((acc, n) => (
path.push(n[0]),
acc.push(...getPaths(n[1], path)),
path.pop(),
acc
), [])
: [ path.join('/') ];
}