document.querySelector('.allstatus-btn').addEventListener('click', () => {
const items = Object.fromEntries(Array.from(
document.querySelectorAll('.number-all'),
n => [ n.innerText, n ]
));
fetch('https://api.novaposhta.ua/v2.0/json/', {
method: 'POST',
body: JSON.stringify({
modelName: 'TrackingDocument',
calledMethod: 'getStatusDocuments',
methodProperties: {
Documents: Object.keys(items).map(n => ({
DocumentNumber: n,
Phone: '',
})),
},
apiKey: '',
}),
})
.then(r => r.json())
.then(r => r.data.forEach(n => items[n.Number].innerHTML += n.Status));
});
function createTree(data) {
const obl = {};
const org = {};
const podr = {};
data.forEach(n => {
obl[n.obl.id] = { ...n.obl, children: [] };
org[n.org.id] = { ...n.org, children: [] };
podr[n.podr.id] = { ...n.podr, children: [] };
});
data.forEach(n => podr[n.podr.id].children.push({ ...n.dolzh }));
Object.values(org).forEach(n => obl[n.parent_obl].children.push(n));
Object.values(podr).forEach(n => org[n.parent_org].children.push(n));
return Object.values(obl);
}
const tree = createTree(arr);
Array.from([...str].reduce((acc, n) => acc.set(n, -~acc.get(n)), new Map), n => n[1] + n[0])
str.split('').sort().join('').match(/(.)\1*/g).map(n => n.length + n[0])
Array.from(new Set(str), n => str.replace(RegExp(`[^${n}]`, 'g'), '').length + n)
const getLastDeepestElement = function(el) {
return this(el, 0)[0];
}.bind(function get(el, level) {
return [...el.children].reduce((p, c) => (
c = get(c, -~level),
p[1] > c[1] ? p : c
), [ el, level ]);
});
function getLastDeepestElement(el) {
let result = [ el, 0 ];
for (const stack = [ result ]; stack.length;) {
const n = stack.pop();
result = n[1] > result[1] ? n : result;
stack.push(...Array.from(n[0].children, m => [ m, n[1] + 1 ]));
}
return result[0];
}
const getDeepestLastElement = el =>
el.lastElementChild
? getDeepestLastElement(el.lastElementChild)
: el;
const getDeepestLastElement = el => Array
.from(el.querySelectorAll('*'))
.pop() || el;
// или
function getDeepestLastElement(el) {
for (let c; (c = el.children).length; el = c[~-c.length]);
return el;
}
.on('click', function(e) {
e.stopPropagation();
const valuesShow = [ 2000, ещё какое-то значение, и ещё, и ещё, ... ];
$('#sample_form_deliv').change(function({ target: { value } }) {
$('#sample_form_pay .input-label:eq(2)').toggle(valuesShow.includes(+value));
});
Array.prototype.reduce.call(
document.querySelector('table').rows,
(acc, n) => {
if (!n.classList.contains('no')) {
acc[acc.length - 1].info.push({
subtitle: n.cells[0].textContent,
cell: n.cells[1].textContent,
});
} else if (!n.querySelector('.no2')) {
acc.push({
title: n.querySelector('.car').textContent,
info: [],
});
}
return acc;
},
[]
)
$(this).parents('.portfolio')
function parents(el, selector) {
const p = [];
while ((el = el.parentNode) !== document && el) {
(!selector || el.matches(selector)) && p.push(el);
}
return p;
}
parents(this, '.portfolio')
$(this).parent('.portfolio')
this.parentNode.classList.contains('portfolio') ? this.parentNode : null
$(this).children('.portfolio')
[...this.children].filter(n => n.classList.contains('portfolio'))
// или
Array.prototype.filter.call(this.children, n => n.matches('.portfolio'))
// или
this.querySelectorAll(':scope > .portfolio')
$(this).find('.portfolio')
this.querySelectorAll('.portfolio')
$(this).next('.portfolio')
(el => el && el.matches('.portfolio') ? el : null)(this.nextElementSibling)
let tr = event.target;
for (; !tr.matches('tr'); tr = tr.parentNode) ;
// или
const tr = event.target.closest('tr');
event.currentTarget.insertBefore(draggableElement, tr.nextElementSibling);
// или
tr.insertAdjacentElement('afterend', draggableElement);
// или
tr.after(draggableElement);
попытался решить все через рекурсию
const createTree = (data, idKey, parentKey, parentId) =>
data.reduce((acc, n) => (
parentId === n[parentKey] && acc.push({
...n,
children: createTree(data, idKey, parentKey, n[idKey]),
}),
acc
), []);
const tree = createTree(data, 'id', 'parentId', null);
function createTree(data, idKey, parentKey) {
const tree = Object.fromEntries(data.map(n => [ n[idKey], { ...n, children: [] } ]));
return Object
.values(tree)
.filter(n => !(tree[n[parentKey]] && tree[n[parentKey]].children.push(n)));
}
const tree = createTree(data, 'id', 'parentId');
const result = array
.concat(obj)
.reduceRight((acc, n) => ({
...n,
children: acc ? [ acc ] : [],
// или, если вдруг исходные массивы непустые,
// и их содержимое надо сохранить
// children: n.children.concat(acc || []),
}), null);
const result = [ ...array, obj ].reduceRight(
(acc, { children, ...n }) => (n.next = acc, n),
null
);
const isEmpty = [...inputs].some(n => !n.value);
// или
const isEmpty = !Array.prototype.every.call(inputs, n => n.value);
// или
let isEmpty = false;
for (const n of inputs) {
if (!n.value) {
isEmpty = true;
break;
}
}
// или
let isEmpty = false;
for (let i = -1; ++i < inputs.length && !(isEmpty = !inputs[i].value);) ;
// или
const isEmpty = (function isEmpty(i, n = inputs.item(i)) {
return !!n && (!n.value || isEmpty(-~i));
})(0);
const grouped = Object.entries(list.reduce((acc, n) => (
(acc[n.part] = acc[n.part] || []).push(n.title),
acc
), {}));
document.body.insertAdjacentHTML('beforeend', grouped
.map(([ k, v ]) => `<div>${k}</div>${v.map(n => `<div>${n}</div>`).join('')}`)
.join('')
);
document.body.append(...grouped.flat(2).map(n => {
const div = document.createElement('div');
div.innerText = n;
return div;
}));
const selector = '.form-group';
const attr = 'data-property-id-row';
const values = [ 10, 13, 15 ];
const elementsToHide = document.querySelectorAll(values
.map(n => `${selector}[${attr}="${n}"]`)
.join(', ')
);
for (let i = 0; i < elementsToHide.length; i++) {
elementsToHide[i].style.transform = 'scale(0)';
}
// или
(function next(i, n = elementsToHide.item(i)) {
n && (n.style.visibility = 'hidden', next(-~i));
})(0);
const elements = document.querySelectorAll(selector);
for (const n of elements) {
n.hidden = values.includes(Number(n.attributes[attr].value));
}
// или (в стили надо будет добавить .hidden { display: none; })
elements.forEach(function(n) {
n.classList.toggle('hidden', this.has(+n.getAttribute(attr)));
}, new Set(values));