почему-то не отрабатывает ни один из хуков
Object.entries(comments).reduce((acc, [ k, v ]) => v.parent === null ? { ...acc, [k]: v } : acc, {})
Object.values(comments).reduce((acc, n) => (n.parent === null && (acc[n.id] = n), acc), {})
<button class="click-count">click me</button>
<button class="click-count">click me</button>
<button class="click-count">click me</button>
document.addEventListener('click', ({ target: t }) => {
if (t.classList.contains('click-count')) {
t.innerText = (t.innerText | 0) + 1;
}
});
// или
const onClick = e => e.target.textContent = -~e.target.textContent;
document.querySelectorAll('.click-count').forEach(n => n.addEventListener('click', onClick));
Есть 3 блока, которые хранятся в массиве
let block = document.querySelectorAll(".block");
const index = Array.prototype.indexOf.call(block, e.target);
const index = [...block].findIndex(n => n === e.target);
let index = block.length;
while (index-- > 0 && block[index] !== e.target) ;
$('blockquote').html((i, html) => html.replace(/(©)(.*)$/, '$1<cite>$2</cite>'));
const el = document.querySelector('blockquote');
const html = el.innerHTML;
const index = html.indexOf('©') + 1;
el.innerHTML = `${html.slice(0, index)}<cite>${html.slice(index)}</cite>`;
const el = document.querySelector('blockquote');
el.innerHTML = el.innerHTML
.split('©')
.map((n, i) => i ? '<cite>' + n + '</cite>' : n)
.join('©');
выдаёт неправильную дату
let ts = 1539338750;
function movie(card, ticket, perc) {
let count = 0;
let price = ticket;
let sum = card;
while (Math.ceil(sum) >= ticket * count) {
count++;
price *= perc;
sum += price;
}
return count;
}
(function timeout() {
if (--container.textContent > to) {
setTimeout(timeout, rand(1000, 4000));
}
})();
const arrayToObject = (array) => { array.reduce(...
return
или убрать фигурные скобки.{id: '1', content: 'Some stuff'},
obj[item._id] = item
как лучше решить задачу преобразования массива в такой объект?
const arrToObj = (arr, key) => arr.reduce((acc, n) => (acc[key(n)] = n, acc), {});
const obj = arrToObj(arr, n => n.id);
arr1.filter(n => arr2.includes(n)).length !== 0
// или
arr1.some(function(n) {
return this.has(n);
}, new Set(arr2))
// или
new Set([ ...arr1, ...arr2 ]).size < new Set(arr1).size + new Set(arr2).size
!!new Set(arr1).intersection(new Set(arr2)).size
<div id="city">
<img src="img/flag-m.png" />
<span>Москва</span>
</div>
document.querySelector('ul').addEventListener('click', e => {
const li = e.target.closest('li');
if (li) {
document.querySelector('#city img').src = li.dataset.gerb;
document.querySelector('#city span').textContent = li.dataset.city;
}
});
// или
document.querySelectorAll('li').forEach(n => {
n.addEventListener('click', onClick);
});
function onClick({ target: { dataset: { city, gerb } } }) {
document.querySelector('#city img').src = gerb;
document.querySelector('#city span').textContent = city;
}
color.bind(null, i)
надо сохранить - в какой-нибудь массив, например.const elements = document.querySelectorAll('.x');
const delay = 1000;
const updateElement = el => el.style.display = 'block';
elements.forEach((n, i) => setTimeout(updateElement, (i + 1) * delay, n));
const intervalId = setInterval(i => {
const el = elements.item(++i[0]);
if (el) {
updateElement(el);
} else {
clearInterval(intervalId);
}
}, delay, [ -1 ]);
(function next(i) {
if (i < elements.length) {
setTimeout(() => {
updateElement(elements[i]);
next(-~i);
}, delay);
}
})(0);