const arr = Array.from(str.matchAll(/[^\/]+/g), n => n[0]);
// или
const arr = str.match(/[^\/]+/g) ?? [];
// или
const arr = str.split('/').filter(Boolean);
// или
const arr = Array.prototype.reduce.call(str, (acc, n, i) => (
(n !== '/') && (
acc[1] !== i - 1 && acc[0].push(''),
acc[0][acc[0].length - 1] += n,
acc[1] = i
),
acc
), [ [], -Infinity ])[0];
<button data-step="-1">-</button>
<input value="1">
<button data-step="+1">+</button>
const values = [ 1, 2, 4, 6, 8, 10, 12, 16, 20, 24, 32, 48, 56, 64 ];
for (const n of document.querySelectorAll('[data-step]')) {
n.addEventListener('click', onClick);
}
function onClick() {
const input = document.querySelector('input');
const index = +this.dataset.step + values.indexOf(+input.value);
input.value = values[Math.max(0, Math.min(values.length - 1, index))];
}
const elements = el.closest('.calc').querySelectorAll('.range__control');
// или
const { elements } = el.form;
const price = Array.prototype.reduce.call(
elements,
(acc, n) => acc * n.value,
1
);
// или
let price = 1;
for (const { value } of elements) {
price *= value;
}
// или
let price = 1;
for (let i = 0; i < elements.length; i++) {
price = price * elements.item(i).value;
}
// или
const price = (function mul(i, n = elements[i]) {
return n ? n.value * mul(-~i) : 1;
})(0);
// или
const price = eval(Array.from(elements, n => n.value).join('*')) ?? 1;
$('select').change(function() { ... }).change();
Object.values(arr.reduce((acc, { id, value }) => {
const type = typeof value;
acc[id] = acc[id] ?? { id };
acc[id][type] = (acc[id][type] ?? 0) + 1;
return acc;
}, {}))
const createTreeElement = (data, index = []) =>
Array.isArray(data) && data.length
? data.reduce((ul, n, i) => (
index.push(i),
ul.append(document.createElement('li')),
ul.lastChild.append(index.join('_'), createTreeElement(n.children, index)),
index.pop(),
ul
), document.createElement('ul'))
: '';
document.body.append(createTreeElement(data));
const createTreeHTML = (data, index = '') =>
data instanceof Array && data.length
? `<ul>${data.map((n, i) => `
<li>
${(i = index + (index && '_') + i)}
${createTreeHTML(n.children, i)}
</li>`).join('')}
</ul>`
: '';
document.body.insertAdjacentHTML('beforeend', createTreeHTML(data));
const result = [];
for (const n of arrWithObj) {
if (!arr.includes(n.id)) {
result.push(n);
}
}
const result = [];
COLLECT_OBJECTS:
for (let i = 0; i < arrWithObj.length; i++) {
for (let j = 0; j < arr.length; j++) {
if (arr[j] === arrWithObj[i].id) {
continue COLLECT_OBJECTS;
}
}
result[result.length] = arrWithObj[i];
}
const result = arrWithObj.filter(function(n) {
return !this.has(n.id);
}, new Set(arr));
const result = (function get(i, n = arrWithObj[i]) {
return n
? [].concat(~arr.indexOf(n.id) ? [] : n, get(-~i))
: [];
})(0);
const result = [...arr.reduce(
(acc, n) => (acc.delete(n), acc),
new Map(arrWithObj.map(n => [ n.id, n ]))
).values()];
const ul = document.querySelector('.ul-parent');
const groupedNodes = Array.prototype.reduce.call(
ul.children,
(acc, n) => {
const k = n.textContent[0].toLowerCase();
(acc[k] = acc[k] ?? []).push(n);
return acc;
},
{}
);
ul.replaceWith(...Object.entries(groupedNodes).map(n => {
const ul = document.createElement('ul');
ul.classList.add('ul-parent', n[0] + '-litter');
ul.append(...n[1]);
return ul;
}));
const ul = document.querySelector('.ul-parent');
const groupedHTML = Array
.from(ul.querySelectorAll(':scope > li'))
.reduce((acc, { innerText: [ c ], outerHTML }) => {
const k = c.toLowerCase();
(acc[k] = acc[k] ?? []).push(outerHTML);
return acc;
}, {});
ul.outerHTML = Object
.entries(groupedHTML)
.map(([ k, v ]) => `<ul class="ul-parent ${k}-litter">${v.join('')}</ul>`)
.join('');
document.addEventListener('click', e => {
const div = e.target.closest('button')?.closest('div');
if (div) {
console.log(div.id);
}
});
document.querySelectorAll('div button').forEach(function(n) {
n.addEventListener('click', this);
}, e => console.log(e.currentTarget.closest('div').id));
arr.sort((a, b) => (a.rooms - b.rooms) || (a.square - b.square));
// или, в более общем виде
const sort = (arr, ...keys) =>
arr.sort((a, b) => {
let diff = 0;
keys.find(k => diff = a[k] - b[k]);
return diff;
});
sort(arr, 'rooms', 'square');
const sorted = (arr, keys) => arr
.map(n => [ n ].concat(keys(n)))
.sort((a, b) => {
let diff = 0;
for (let i = 0; ++i < a.length && !(diff = a[i] - b[i]);) ;
return diff;
})
.map(n => n[0]);
const sortedArr = sorted(arr, n => [ n.rooms, n.square ]);
const $table = $('table');
const columnIndex = // здесь должен быть номер столбца с числами
const sum = $table
.find('tr:visible')
.get()
.reduce((acc, n) => acc + +$('td', n).eq(columnIndex).text(), 0);
$table
.find('th')
.eq(columnIndex)
.text(`Вес (${sum})`);
const parents = document.querySelectorAll('.news-block-2 tbody tr');
const swapChildren = function(el, selectorA, selectorB) {
const a = this(el.children, n => n.matches(selectorA));
const b = this(el.children, n => n.matches(selectorB));
el.append(...Array.from(el.children, n => n === a ? b : n === b ? a : n));
}.bind(Function.prototype.call.bind(Array.prototype.find));
parents.forEach(n => swapChildren(n, '.picture-news', '.text-news'));
const swapChildren = el => el.append(...[...el.childNodes].reverse());
// или
const swapChildren = el => el.appendChild(el.firstElementChild);
// или
const swapChildren = el => el.insertBefore(el.children[0], null);
// или
const swapChildren = el => el.insertAdjacentElement('beforeend', el.children[0]);
// или
const swapChildren = el => el.prepend(el.querySelector(':scope > :last-child'));
// или
const swapChildren = ({ lastElementChild: el }) => el.previousElementSibling.before(el);
// или
const swapChildren = el => el.cells[1].after(el.cells[0]);
// или
const swapChildren = ({ cells: [ a, b ] }) => a.replaceWith(b, a);
parents.forEach(swapChildren);
// или
for (const n of parents) {
swapChildren(n);
}
// или
for (let i = 0; i < parents.length; i++) {
swapChildren(parents[i]);
}
// или
(function next(i, n = parents.item(i)) {
n && (swapChildren(n), next(-~i));
})(0);
X = n => n ? (n & 1 ? '-chirp' : '') + X(n >> 1) + X(n >> 1) : ''
chirp = n => X(n).slice(1)
google.visualization.arrayToDataTable(data, true)