const obj = [...str.matchAll(/([^?&]+)=([^&]+)/g)]
.reduce((acc, [ , k, v ]) => (
k.endsWith('[]')
? (acc[k.slice(0, -2)] ??= []).push(v)
: acc[k] = v,
acc
), {});
const params = new URLSearchParams(str);
const reg = /\[\]$/;
const obj = Object.fromEntries(Array.from(
new Set(params.keys()),
n => [
n.replace(reg, ''),
params[reg.test(n) ? 'getAll' : 'get'](n),
]
));
const obj = str.split('?').pop().split('&').reduce((acc, n) => {
let [ k, v ] = n.split('=');
const isArr = k.slice(-2) === '[]';
k = k.substring(0, k.length - isArr * 2);
const target = (isArr ? (acc[k] = acc[k] || []) : acc);
target[isArr ? target.length : k] = v;
return acc;
}, {});
const key = 'strategy';
.const result = arr.filter(function(n) {
return this.has(n[key]);
}, new Set(arr2));
const result = arr2.flatMap(((values, n) => values[n] ?? []).bind(
null,
arr.reduce((acc, n) => ((acc[n[key]] ??= []).push(n), acc), {})
));
const result = [];
for (const n of arr) {
for (const m of arr2) {
if (m === n[key]) {
result.push(n);
break;
}
}
}
const result = [];
for (let i = 0; i < arr.length; i++) {
if (~arr2.indexOf(arr[i][key])) {
result[result.length] = arr[i];
}
}
const result = (function get(i, n = arr[i]) {
return n
? [].concat(arr2.includes(n[key]) ? n : [], get(-~i))
: [];
})(0);
const key = 'number';
const attr = `data-${key}`;
const attrSelector = `[${attr}]`;
document.addEventListener('click', e => {
const value = e.target.closest(attrSelector)?.dataset[key];
if (value) {
document.querySelectorAll(`[${attr}="${value}"]`).forEach(n => n.remove());
}
});
// или
const elems = [...document.querySelectorAll(attrSelector)];
const onClick = ({ currentTarget: { attributes: { [attr]: { value } } } }) =>
elems.length -= elems.reduce((acc, n, i, a) => (
a[i - acc] = n,
acc + (n.getAttribute(attr) === value && !n.replaceWith())
), 0);
elems.forEach(n => n.addEventListener('click', onClick));
function haveSameValues(arr1, arr2) {
if (arr1.length !== arr2.length) {
return false;
}
const count = new Map;
arr1.forEach(n => count.set(n, -~count.get(n)));
arr2.forEach(n => count.set(n, ~-count.get(n)));
for (const n of count.values()) if (n) {
return false;
}
return true;
}
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('');
for (let n = null; n = wrapper.lastChild; n.remove()) ;
for (const n of word.value) {
if (obj.hasOwnProperty(n)) {
const img = new Image;
img.src = obj[n];
wrapper.append(img);
}
}
// или
wrapper.replaceChildren(...Array.prototype.reduce.call(
word.value,
(acc, n) => (
obj[n] && ((acc[acc.length] = new Image).src = obj[n]),
acc
),
[]
));
while (word.value.length < wrapper.children.length) {
wrapper.removeChild(wrapper.lastElementChild);
}
while (word.value.length > wrapper.children.length) {
wrapper.appendChild(document.createElement('img'));
}
Array.prototype.forEach.call(wrapper.children, (n, i) => {
const src = obj[word.value[i]];
if (!(n.hidden = !src)) {
n.src = src;
}
});
let obj = { 'а': 'https://github.com/itsFide/converter/blob/master/img/а.png?raw=true', 'А': 'https://github.com/itsFide/converter/blob/master/img/а.png?raw=true', 'б':'https://github.com/itsFide/converter/blob/master/img/б.png?raw=true', ...
const obj = Object.fromEntries(Array.prototype.flatMap.call(
'абвгдеёжзийклмнопрстуфхцчшщъыьэюя', n => {
const url = `https://github.com/itsFide/converter/blob/master/img/${n}.png?raw=true`;
return [
[ n, url ],
[ n.toUpperCase(), url ],
];
}
));
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('/') ];
}
function shortNumber(val) {
const abs = Math.abs(val);
const prefixIndex = Math.log10(abs) / 3 | 0;
return (
(val < 0 ? '-' : '') +
Math.round(abs / (10 ** (prefixIndex * 3))) +
'KMGTPEZY'.charAt(~-prefixIndex)
);
}
shortNumber(99) // '99'
shortNumber(1945) // '2K'
shortNumber(-5839465) // '-6M'
shortNumber(7e10) // '70G'
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());
const elements = document.querySelectorAll('.descr');
const addText = (el, index) =>
el.append(arr[index]);
// или
// el.replaceChildren(arr[index]);
// el.textContent = arr[index];
// el.innerText = arr[index];
// el.innerHTML = arr[index];
// el.appendChild(document.createTextNode(arr[index]));
// el.insertBefore(new Text(arr[index]), null);
// el.insertAdjacentText('beforeend', arr[index]);
// el.insertAdjacentHTML('beforeend', arr[index]);
elements.forEach(addText);
// или
for (const [ i, n ] of elements.entries()) {
addText(n, i);
}
// или
for (let i = 0; i < elements.length; i++) {
addText(elements[i], i);
}
// или
(function next(i, n = elements.item(i)) {
n && (addText(n, i), next(-~i));
})(0);
`${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, '')
$('#search-faq-input-filter').on('input', e => {
const val = $(e.target).val().toLowerCase();
$('.ia-item')
.hide()
.filter((i, n) => (
$('.ia-content', n).text().toLowerCase().includes(val) ||
$('.ia-title-link', n).text().toLowerCase().includes(val)
))
.show();
});
[...new Set(data.variations.map(n => n.color.name))]
Object.values(Object.fromEntries(data.variations.map(n => [ n.color.name, n.color ])))
// или
Object.values(data.variations.reduce((acc, { color: n }) => (acc[n.name] ??= n, acc), {}))
// или
data.variations.map(n => n.color).filter(function(n) {
return !(this[n.name] = this.hasOwnProperty(n.name));
}, {})
function unique(data, key = n => n) {
const getKey = key instanceof Function ? key : n => n[key];
const keys = new Set;
const result = [];
for (const n of data) {
const k = getKey(n);
!keys.has(k) && keys.add(k) && result.push(n);
}
return result;
}
// получаем массив уникальных имён цветов
const uniqueStrColors = unique(data.variations.map(n => n.color.name));
// получаем массив объектов цветов, свойства name которых уникальны
const uniqueObjColors = unique(data.variations.map(n => n.color), n => n.name);
const elements = document.querySelectorAll('.slider__itm img');
const tag = 'a';
elements.forEach(n => {
n.after(document.createElement(tag));
n.nextSibling.append(n);
});
for (const n of elements) {
const wrapper = document.createElement(tag);
wrapper.appendChild(n.parentNode.replaceChild(wrapper, n));
}
for (let i = 0; i < elements.length; i++) {
const wrapper = document.createElement(tag);
elements[i].replaceWith(wrapper);
wrapper.insertAdjacentElement('afterbegin', elements[i]);
}
(function next(i, n = elements.item(i)) {
n && (n.outerHTML = `<${tag}>${n.outerHTML}</${tag}>`, next(-~i));
})(0);
{ точка: сегмент }
). Находим начальный сегмент маршрута - такой, начальная точка которого не является ничьей конечной. Следующий сегмент маршрута - такой, начальная точка которого является конечной точкой текущего сегмента. Ну и крутим цикл до тех пор, пока текущий сегмент маршрута существует, не забывая сохранять его в результирующий массив:function sort(route) {
const pointsFrom = Object.fromEntries(route.map(n => [ n.from, n ]));
const pointsTo = Object.fromEntries(route.map(n => [ n.to, n ]));
const sorted = [];
for (
let segment = route.find(n => !pointsTo[n.from]);
segment;
segment = pointsFrom[segment.to]
) {
sorted.push(segment);
}
return sorted;
}
const remove = str => str.split(' ').slice(0, -1).join(' ');
// или
const remove = str => str.replace(/\s\S+$/, '');
// или
const remove = str => str.match(/.+(?=\s)/);
// или
const remove = str => str.slice(0, str.lastIndexOf(' '));
for (const n of document.getElementsByClassName('startdate')) {
n.textContent = remove(n.textContent);
}
// или, раз уж полной даты уже не будет, пусть начало и конец интервала дат
// располагаются внутри одного элемента - вместе с куском строки также
// удаляем и родительский элемент
document.querySelectorAll('.startdate').forEach(n => {
n.outerHTML = remove(n.innerHTML);
// или
n.replaceWith(remove(n.innerText));
});