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('/') ];
}
const replaceKeys = (val, replacer) =>
val instanceof Array
? val.map(n => replaceKeys(n, replacer))
: val instanceof Object
? Object.fromEntries(Object
.entries(val)
.map(n => [ replacer(n[0]), replaceKeys(n[1], replacer) ])
)
: val;
const newObj = replaceKeys(obj, k => k.toUpperCase());
`${str | 0}`
// или
str.replace(/^0+/, '')
// или
str.match(/[^0].*/)[0]
// или
str.split(/^0*/).pop()
// или
str.slice(str.search(/[^0]/))
// или
[...str].reduce((acc, n) => (+n || acc) && acc + n, '')