const duplicates = Array
.from(arr.reduce((acc, n) => acc.set(n.id, acc.has(n.id)), new Map))
.reduce((acc, n) => (n[1] && acc.push(n[0]), acc), []);
// или
const duplicates = Object
.entries(arr.reduce((acc, n) => (acc[n.id] = (acc[n.id] || 0) + 1, acc), {}))
.filter(n => n[1] > 1)
.map(n => +n[0]);
// или
const duplicates = [...arr
.reduce((acc, n) => (acc[+acc[0].has(n.id)].add(n.id), acc), [ new Set, new Set ])
.pop()
];
// или
const duplicates = arr
.map(n => n.id)
.filter((n, i, a) => i !== a.indexOf(n))
.filter((n, i, a) => i === a.indexOf(n));
// или
const duplicates = arr.reduce((acc, { id: n }, i, a) => (
!acc.includes(n) && i !== a.findIndex(m => m.id === n) && acc.push(n),
acc
), []);
$('.slider').slider('values', [ thisfrom, thisto ]);
const colors = [ 'red', 'orange', 'yellow', 'green', 'aqua', 'blue', 'magenta' ];
let index = -1;
+ 1
и меняете цвет background'а:hoverEl.addEventListener('mouseenter', () => {
index = (index + 1) % colors.length;
colorEl.style.backgroundColor = colors[index];
});
const data = Array
.from(document.querySelectorAll('.js-input input'))
.reduce((acc, n) => {
const keys = n.name.match(/(?<=\[)\w+(?=\])/g);
const key = keys.pop();
keys.reduce((p, c) => p[c] = p[c] || {}, acc)[key] = n.value;
return acc;
}, {});
т.к. объектов много (~2000) при очистке фильтра страница подлагивает
const videos = document.querySelectorAll('video');
const onPlay = e => videos.forEach(n => n !== e.target && n.pause());
videos.forEach(n => n.addEventListener('play', onPlay));
const str = arr
.map(n => `${n[0].toUpperCase()}${n.slice(1)}`)
.join(', ');
// или
const str = arr.reduce((acc, n) => acc + (acc && ', ') + n.replace(/./, m => m.toUpperCase()), '');
// или
const str = arr
.map(n => ''.concat(...Array.from(n, (m, i) => i ? m : m.toUpperCase())))
.join`, `;
есть массив индексов[0, 3, 1, empty, 2]
<...>
должно быть -[100, 400, 200, empty, 300]
for (let i = 0; i < indexes.length; i++) {
if (indexes.hasOwnProperty(i)) {
arr.push(arr[indexes[i]]);
} else {
arr.length++;
}
}
arr.splice(0, arr.length - indexes.length);
arr.splice(0, arr.length, ...indexes.map(i => arr[i]));
const className = 'carusel__item_active';
.const hasClass = el => el.classList.contains(className);
// или
const hasClass = el => el.matches(`.${className}`);
// или
const hasClass = el => el.className.split(' ').includes(className);
// или
const hasClass = el => RegExp(`(^| )${className}( |$)`).test(el.classList.value);
const activeSlide = Array.prototype.find.call(items, hasClass);
// или
let activeSlide = null;
for (const n of items) {
if (hasClass(n)) {
activeSlide = n;
break;
}
}
// или
const [ activeSlide ] = [...items].filter(hasClass);
// или
let activeSlide = null;
for (let i = 0; i < items.length && !activeSlide; i++) {
activeSlide = hasClass(items[i]) ? items[i] : activeSlide;
}
function onMarkerClick(e) {
map.setZoom(8);
map.panTo(e.latLng);
}
for (const f of features) {
const marker = new google.maps.Marker({
position: f.position,
icon: icons[f.type].icon,
map,
});
marker.addListener('click', onMarkerClick);
}
const input = document.querySelector('input');
const buttons = document.querySelectorAll('button');
buttons.forEach(n => n.addEventListener('click', onClick));
const classes = [
[ 'id1', 'class1' ],
[ 'id2', 'class2' ],
[ 'id3', 'class3' ],
];
function onClick() {
classes.forEach(n => input.classList.toggle(n[1], n[0] === this.id));
}
<button data-class="class1">click me</button>
<button data-class="class2">click me</button>
<button data-class="class3">click me</button>
function onClick() {
buttons.forEach(n => input.classList.toggle(n.dataset.class, n === this));
}
const count = (max, timeout) => (function next(i) {
if (i <= max) {
setTimeout(() => {
console.log(i);
next(++i);
}, timeout(i));
}
})(0);
count(100, i => i * 5);
function debounce(f, delay) {
let timeoutId = null;
return function() {
clearTimeout(timeoutId);
timeoutId = setTimeout(() => f.apply(this, arguments), delay);
};
}
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.values(tasks.reduce((acc, { user: { id, name }, ...n }) => {
(acc[id] = acc[id] || { user: { id, name, tasks: [] } }).user.tasks.push(n);
return acc;
}, {}))