const index = arr.findIndex(n => n.id === id);
if (index !== -1) {
arr.splice(index, 1);
}
const newArr = arr.filter(n => n.id !== id);
for (let i = arr.length; i--;) {
if (arr[i].id === id) {
arr.splice(i, 1);
}
}
reduceRight
используется не совсем по назначения, как и оператор &&
):arr.reduceRight((_, n, i, a) => n.id === id && a.splice(i, 1), null);
let countDeleted = 0;
for (let i = 0; i < arr.length; i++) {
arr[i - countDeleted] = arr[i];
countDeleted += arr[i].id === id;
}
arr.length -= countDeleted;
arr.splice(0, arr.length, ...arr.filter(n => n.id !== id));
<input type="range" min="20" max="200" value="100">
<img src="...">
document.querySelector('input').addEventListener('input', function() {
document.querySelector('img').style.filter = `brightness(${this.value}%)`;
});
$('.box').each((i, n) => {
const $blocks = $('.section .block', n);
$('.imgs img', n).each((i, n) => $blocks.eq(i).append(n));
});
document.querySelectorAll('.box').forEach(n => {
const images = n.querySelectorAll('img');
n.querySelectorAll('.block').forEach((n, i) => n.append(images[i]));
});
return this.data();
const chunked = (data, chunkSize) =>
Array.prototype.reduce.call(
data,
(acc, n, i) => (
i = i / chunkSize | 0,
(acc[i] = acc[i] || []).push(n),
acc
),
[]
);
console.log(chunked([...Array(10).keys()], 3));
console.log(chunked('ABCDEFG', 2));
console.log(chunked(document.querySelectorAll('img'), 5));
for (let i = 0; i < answer.length - 1; i++)
for (let i = 0; i < words.length; i++)
p.style.color = colors[i]
. function throttle(f, delay) {
let lastCall = -Infinity;
return function() {
const now = +new Date;
if (now - lastCall > delay) {
lastCall = now;
return f.apply(this, arguments);
}
};
}
Object.entries(props).filter(n => n[1]).map(n => n[0]).join(',')
// или
`${Object.entries(props).reduce((acc, [ k, v ]) => (v && acc.push(k), acc), [])}`
// или
Object.entries(props).map(n => n[1] && n[0]).filter(Boolean).toString()
// или
'' + Object.keys(props).reduce((acc, n) => props[n] ? [ ...acc, n ] : acc, [])
// или
String(Object.keys(props).filter(n => props[n]))
const colors = [ 'red', 'lime', 'yellow', 'aqua', 'brown', 'magenta' ];
$('.item').css('background-color', i => colors[i % colors.length]);
// или
document.querySelectorAll('.item').forEach((n, i) => {
n.style.backgroundColor = colors[i % colors.length];
});
this.sliderPoint.on('click', e => {
this.currentImg = e.target.dataset.id;
this.scrollImages(this.currentImg, this.speed);
});
[...Array(count)].map((n, i) => `<div class="slider-point" data-id="${i}"></div>`).join('')
Array(count).fill('<div class="slider-point"></div>').join('')
this.currentImg = $(e.target).index();
document.addEventListener('click', e => {
const slide = e.target.closest('.slide');
if (slide) {
const index = Array.prototype.indexOf.call(
slide.parentNode.children,
slide
);
console.log(index);
}
});
for (const n of document.querySelectorAll('.slide')) {
n.addEventListener('click', onClick);
}
function onClick() {
let index = 0;
for (let el = this; el = el.previousElementSibling; index++) ;
console.log(index);
}
document.querySelectorAll('.slider').forEach(function({ children }) {
[].forEach.call(children, (n, i) => {
n.dataset.index = i;
n.addEventListener('click', this);
});
}, e => console.log(+e.currentTarget.dataset.index));
const slidesCount = this.slider.find('.slide').length;
// или
const { length: slidesCount } = this.slider.children();
const dotHTML = '<div class="slider-point"></div>';
const dotsHTML = Array(slidesCount).fill(dotHTML).join('');
// или
const dotsHTML = Array(slidesCount + 1).join(dotHTML);
// или
const dotsHTML = dotHTML.repeat(slidesCount);
this.slider.siblings('.slider-pagination').append(dotsHTML);
// или
this.slider.closest('.slider-box').find('.slider-pagination').html(dotsHTML);
this.previousImage = this.previousImage.bind(this);
this.nextImage = this.nextImage.bind(this);
this.prevBtn = this.slider.siblings(".slider-btn.previous");
this.nextBtn = this.slider.siblings(".slider-btn.next");
if($(this).children().is(':checked')){ $(this).addClass('active'); } else { $(this).removeClass('active'); }
$(this).addClass('active').siblings().removeClass('active');