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 = 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 = Object.values(arr.reduce(
(acc, n) => (delete acc[n], acc),
Object.fromEntries(arrWithObj.map(n => [ n.id, n ]))
));
const result = Array.from(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)
Array
.from(document.querySelectorAll('.obj'))
.filter(n => n.querySelector('.who')?.innerText !== 'Я')
.forEach(n => console.log(n.innerText));
const itemSelector = 'здесь должен селектор элементов, которые надо переместить';
const containerSelector = 'а здесь - селектор контейнеров';
const chunkSize = 3;
const $items = $(itemSelector);
$(containerSelector).append(i => $items.slice(i * chunkSize, (i + 1) * chunkSize));
// или
const $containers = $(containerSelector);
$(itemSelector).each((i, n) => $containers.eq(Math.floor(i / chunkSize)).append(n));
document.querySelectorAll(containerSelector).forEach(function(n, i) {
n.append(...this.slice(i * chunkSize, -~i * chunkSize));
}, [...document.querySelectorAll(itemSelector)]);
// или
const containers = document.querySelectorAll(containerSelector);
for (const [ i, n ] of document.querySelectorAll(itemSelector).entries()) {
containers[i / chunkSize | 0]?.appendChild(n);
}
$('label > input').change(function() {
$(this).parent().toggleClass('active', this.checked);
});