// можно посмотреть количество ключей
const isEmpty = x => !Object.keys(x || {}).length;
// или перебирать свойства, пока не встретится собственное
function isEmpty(x) {
for (const k in x) if (x.hasOwnProperty(k)) {
return false;
}
return true;
}
isEmpty() // true
isEmpty(null) // true
isEmpty(666) // true
isEmpty('') // true
isEmpty([]) // true
isEmpty({}) // true
isEmpty([ 187 ]) // false
isEmpty({ xxx: 69 }) // false
isEmpty('hello, world!!') // false
await Promise.all([ one(), query().then(commit) ])
await Promise.all([ one(), query().then(r => (commit(), r)) ])
const data = Array.from(
document.querySelectorAll('.bigDiv'),
n => Array.from(n.querySelectorAll('.smallDiv'), m => m.innerText)
);
const data = [];
for (const n of document.getElementsByClassName('bigDiv')) {
data.push([]);
for (const m of n.children) {
data[data.length - 1].push(m.innerHTML);
}
}
const data = Array.prototype.reduce.call(
document.getElementsByClassName('smallDiv'),
(acc, n) => (
n.previousElementSibling || acc.push([]),
acc[~-acc.length].push(n.textContent),
acc
),
[]
);
.hidden {
display: none;
}
const checkbox = document.querySelector('.block50 input');
const block = document.querySelector('.block83');
const onChange = e => block.classList.toggle('hidden', !e.target.checked);
checkbox.addEventListener('change', onChange);
список скрывается только после того, когда поставишь и уберешь галочку
<div class="block83 hidden">
checkbox.dispatchEvent(new Event('change'));
body:not(:has(.block50 :checked)) .block83 {
display: none;
}
document.querySelector('#myselect').addEventListener('change', function(e) {
const select = this;
// или
// const select = e.target;
// const select = e.currentTarget;
const [ option ] = select.selectedOptions;
// или
// const option = select[select.selectedIndex];
// const option = select.querySelector(`[value="${select.value}"]`);
// const option = select.querySelector('option:checked');
// const option = [...select.options].find(n => n.selected);
const forAttr = option.getAttribute('for');
// или
// const forAttr = option.attributes.for.value;
document.querySelector('#mydiv').innerText = `Вес брутто*: ${forAttr}`;
});
setValue(prevValue => prevValue + number);
setValue(prevValue => (prevValue + number).slice(0, 3));
Promise
.all([...img].map(n => new Promise(r => n.complete ? r() : n.onload = r)))
.then(() => img.forEach(n => n.style.position = 'absolute'));
const intervals = [ 1000, 2000, 500, 3000 ];
let interval = -1;
function newsRotator() {
const $news = $('.news');
const $hidden = $news.not(':visible');
const $next = $hidden.eq(Math.random() * $hidden.length | 0);
$news.hide();
$next.show();
interval = (interval + 1) % intervals.length;
setTimeout(newsRotator, intervals[interval]);
}
newsRotator();
for(i = 0
i
должна быть объявлена. Погуглите, что происходит, если выполнить присваивание необъявленной переменной.i<= n;
newArr[i] = a[i];
function arrayNtimes(a, n) {
const newArr = [];
for (let i = 0; i < n * a.length; i++) {
newArr[i] = a[i % a.length];
}
return newArr;
}
const arrayNtimes = (a, n) => Array(n).fill(a).flat();
element.insertAdjacentHTML('beforebegin', html);
element.before(...new DOMParser().parseFromString(html, 'text/html').body.childNodes);
// или
element.parentNode.insertBefore(document.createRange().createContextualFragment(html), element);
element.outerHTML = html + element.outerHTML;