const className = 'qq-upload-file';
.const elements = document.querySelectorAll(`.${className}`);
// или
const elements = document.getElementsByClassName(className);
const getText = el => el.textContent;
// или
const getText = el => el.innerText;
// или (т.к., вложенных элементов нет)
const getText = el => el.innerHTML;
const texts = Array.from(elements, getText);
// или
const texts = Array.prototype.map.call(elements, getText);
// или
const texts = [];
for (const n of elements) {
texts.push(getText(n));
}
// или
const texts = [];
for (let i = 0; i < elements.length; i++) {
texts[i] = getText(elements[i]);
}
const uniqueCount = new Set(texts).size;
// или
const { size: uniqueCount } = new Map(texts.map(n => [ n, n ]));
// или
const [ uniqueCount ] = texts.reduce((acc, n) => {
acc[0] += !(acc[1][n] = acc[1].hasOwnProperty(n));
return acc;
}, [ 0, {} ]);
// или
const uniqueCount = texts.filter((n, i, a) => i === a.indexOf(n)).length;
const inputs = document.querySelectorAll('form input');
// или
const inputs = document.forms[0].elements;
// или
const inputs = document.querySelector('form').getElementsByTagName('input');
const getName = el => el.name;
// или
const getName = el => el.getAttribute('name');
// или
const getName = el => el.attributes.name.value;
const names = Array.from(inputs, getName);
// или
const names = Array.prototype.map.call(inputs, getName);
// или
const names = [];
for (const n of inputs) {
names.push(getName(n));
}
// или
const names = [];
for (let i = 0; i < inputs.length; i++) {
names[i] = getName(inputs[i]);
}
const result = Object
.entries(arr.reduce((acc, n, i) => ((acc[n] = acc[n] || []).push(i), acc), {}))
.map(([ label, indexes ]) => indexes.length > 1 ? { label, indexes } : label);
const result = Object.values(arr.reduce((acc, n) => (
(acc[n.ID] = acc[n.ID] || { ID: n.ID })[n.FIELD] = n.VALUE,
acc
), {}));
setInterval(count => {
const
len = count.length,
val = `${+count.map(n => n.textContent).join('') + 1}`.padStart(len, 0).slice(-len);
count.forEach((n, i) => n.textContent = val[i]);
}, 50, [...document.querySelectorAll('.count')]);
document.querySelectorAll('div[name] ul[name]').forEach(n => {
if (n.getAttribute('name') !== n.closest('div[name]').getAttribute('name')) {
n.remove();
}
});
for (const n of document.querySelectorAll('div[name]')) {
for (const m of n.querySelectorAll(`ul:not([name="${n.attributes.name.value}"])`)) {
m.parentNode.removeChild(m);
}
}
const sorted = (arr, key) => arr
.map(n => [ n, key(n) ])
.sort((a, b) => a[1] - b[1])
.map(n => n[0]);
// если элементы с отсутствующим номером квартиры должны оказаться
// в начале, а не в конце, то вместо Infinity надо поставить 0
const sortedArr = sorted(arr, n => +(n.address.match(/кв.\s*(\d+)/) || [ Infinity ]).pop());
ul
и текущий уровень. Берутся все элементы до следующего с текущим уровнем, оборачиваются в ul
, затем следует рекурсивный вызов: свежесозданный ul
, текущий уровень + 1.ul
содержит дочерние ul
:function wrap(ul, level = 0) {
const
currLevel = `.level-${level}`,
nextLevel = `.level-${level + 1}:first`;
let $li = null;
while (($li = $(ul).children(nextLevel)).length) {
const $ul = $('<ul></ul>');
$ul.append($li.nextUntil(currLevel).addBack().last().after($ul).end());
wrap($ul, level + 1);
}
}
ul
в li
текущего уровня, находящиеся перед элементами следующего уровня:function wrap(ul, level = 0) {
const
currLevel = `.level-${level}`,
nextLevel = `.level-${level + 1}:first`;
let $li = null;
while (($li = $(ul).children(nextLevel)).length) {
const $ul = $('<ul></ul>');
$li.prev().append($ul);
$ul.append($li.nextUntil(currLevel).addBack());
wrap($ul, level + 1);
}
}
for (let i = arr.length; i--;) {
const exists = arr.hasOwnProperty(i);
if (exists || !i) {
arr.length = i + exists;
break;
}
}
for (let i = arr.length; i-- && !(i in arr); arr.pop()) ;
str = str[0].toUpperCase() + str.slice(1);
// или
str = str.replace(/^./, m => m.toUpperCase());
// или
str = Array.from(str, (n, i) => i ? n : n.toUpperCase()).join('');
const names = [ 'DIV', 'P', 'SPAN', ... ];
if (names.includes(el.nextSibling.nodeName)) {
...
-1
- добавить значение в массив, в противном случае - удалить:const active = [];
document.addEventListener('click', ({ target: t }) => {
const value = t.dataset['data-атрибут'];
if (value) {
const index = active.indexOf(value);
if (index === -1) {
active.push(value);
} else {
active.splice(index, 1);
}
}
});
document.addEventListener('click', ({ target: t }) => {
if (t.dataset['data-атрибут']) {
t.classList.toggle('active');
}
});
const active = Array
.from(document.querySelectorAll('.active'), n => n.dataset['data-атрибут'])
.join(', ');
const sourceKey = 'population';
const sourceVal = 2870528;
const targetKey = 'name';
const [ val ] = jp.query(cities, `$[?(@.${sourceKey} == ${sourceVal})].${targetKey}`);
const val = (cities.find(n => n[sourceKey] === sourceVal) || {})[targetKey];
'use strict';
Может кто то знает как решить эту проблему?