document.querySelector('input').addEventListener('input', e => {
const
val = e.target.value,
reg = RegExp(val, 'gi'),
keys = [ 'firstName', 'lastName' ];
document.querySelector('.autocomplete-suggestions__list').innerHTML = val && people
.map(n => keys.map(k => n[k]).join(' '))
.filter(n => (reg.lastIndex = 0, reg.test(n)))
.map(n => `<div>${n.replace(reg, '<strong>$&</strong>')}</div>`)
.join('');
});
.html($(this).find('img'))
.html($(this).find('img').clone())
.items-left
, а проверяете количество .items-right
. Думали, что класс сам собой заменится? Нет, так не будет. const getImg = url => fetch(url)
.then(r => r.blob())
.then(blob => new Promise(resolve => {
const reader = new FileReader();
reader.addEventListener('loadend', () => resolve({ image: reader.result }));
reader.readAsDataURL(blob);
}));
document.querySelector('button').addEventListener('click', async () => {
const arrImg = await Promise.all(arr.map(n => getImg(`./images/${n}`)));
console.log(arrImg);
});
const $items = $('.card');
const openedClass = 'card--opened';
const closedClass = 'card--closed';
const delay = 500;
const flip = $item => $item.removeClass(closedClass).addClass(openedClass);
$items.each((i, n) => setTimeout(flip, (i + 1) * delay, $(n)));
(function next(i) {
if (i < $items.length) {
setTimeout(() => {
flip($items.eq(i));
next(-~i);
}, delay);
}
})(0);
let i = 0;
const intervalID = setInterval(() => {
flip($items.eq(i));
if (++i >= $items.length) {
clearInterval(intervalID);
}
}, delay);
const obj = array.reduce((acc, n, i) => (
acc[`filter${i + 1}`] = { ...baseFilter, prop: n },
acc
), {});
const obj = {};
for (const [ i, n ] of array.entries()) {
(obj['filter' + (-~i)] = Object.assign({}, baseFilter)).prop = n;
}
const obj = {};
for (let i = 0; i < array.length;) {
const subobj = { prop: array[i] };
for (const k in baseFilter) {
subobj[k] = baseFilter[k];
}
obj['filter'.concat(++i)] = subobj;
}
const [ arr1, arr2 ] = arr.list
.map(n => [ n, +n.pos.dv.slice(1) ])
.sort((a, b) => a[1] - b[1])
.reduce((acc, n) => (acc[+(n[1] >= 38)].push(n[0]), acc), [ [], [] ])
.map(n => ({ list: n }));
.red {
background: red;
}
$('.table').on('change', function(e) {
const $tr = $(e.target).closest('tr');
const ajaxdata = $tr.find('.ajaxdata').val();
const subcat = $tr.find('.subcat').val();
$tr.toggleClass('red', ajaxdata === 'none' && subcat === 'none');
}).find('tr').change();
// или
const table = document.querySelector('table');
table.addEventListener('change', e => {
const tr = e.target.closest('tr');
const ajaxdata = tr.querySelector('.ajaxdata').value;
const subcat = tr.querySelector('.subcat').value;
tr.classList.toggle('red', ajaxdata === 'none' && subcat === 'none');
})
table.querySelectorAll('tr').forEach(n => {
n.dispatchEvent(new Event('change', { bubbles: true }));
});
const mustStay = arr => arr.every(n => n.value !== '-');
const newArr = arr.filter(mustStay);
arr.reduceRight((_, n, i, a) => mustStay(n) || a.splice(i, 1), null);
// или
arr.splice(0, arr.length, ...arr.filter(mustStay));
// или
let numDeleted = 0;
for (const [ i, n ] of arr.entries()) {
arr[i - numDeleted] = n;
numDeleted += !mustStay(n);
}
arr.length -= numDeleted;
const length = Math.max(...arr.map(n => n.length));
for (const n of arr) {
const id = 1 + Math.max(0, ...n.map(m => m.id));
n.push(...Array.from(
{ length: length - n.length },
(_, i) => ({ id: id + i, value: '_' })
));
}
const newArr = arr.map(n => Array.from({ length }, (_, i) => ({
id: i + 1,
value: i < n.length ? n[i].value : '-',
})));
[ 7, 7, 7, 0, 1, 1 ]
? В первом случае 2
- повторяются семёрка и единица, во втором 5
- три семёрки плюс две единицы.const duplicateCount = Object
.values(arr.reduce((acc, n) => (acc[n] = (acc[n] || 0) + 1, acc), {}))
.filter(n => n > 1)
.length;
// или
const duplicateCount = Array
.from(arr.reduce((acc, n) => acc.set(n, acc.has(n)), new Map).values())
.reduce((acc, n) => acc + n, 0);
// или
const duplicateCount = new Set(arr.filter((n, i, a) => i !== a.indexOf(n))).size;
const duplicateCount = Object
.values(arr.reduce((acc, n) => (acc[n] = acc.hasOwnProperty(n), acc), {}))
.reduce((acc, n) => acc - !n, arr.length);
// или
const duplicateCount = Array
.from(arr.reduce((acc, n) => acc.set(n, -~acc.get(n)), new Map).values())
.reduce((acc, n) => acc + (n > 1) * n, 0);
// или
const duplicateCount = arr
.filter((n, i, a) => a.indexOf(n) !== a.lastIndexOf(n))
.length;
const isArrOK = arr => arr.length && arr.every(n => n.value !== '-');
const newArr = arr.reduce((acc, { arr: n }) => (
isArrOK(n) && acc.push(n),
acc
), []);
// или
const newArr = arr.map(n => n.arr).filter(isArrOK);
// или
const newArr = [];
for (const n of arr) {
if (isArrOK(n.arr)) {
newArr.push(n.arr);
}
}
$('#file').on('change', ({ target: { files: [ file ] } }) => {
$('#info-file').html([ 'name', 'size', 'type' ]
.map(n => `<div>${n}: ${file[n]}</div>`)
.join('')
);
});
$("#box1:checked, #box2:checked").val()
$('#logistic input:checked').get().reduce((acc, n) => acc + +n.value, 0)
function GetCookies(name) { <...> return null; } <...> faves.push(fav); <...> var myfaves = JSON.parse(GetCookies('favespages')); faves = myfaves;
faves = myfaves instanceof Array ? myfaves : [];
. const result = tags.filter(n => active.some(m => m.name === n.name));
const result = tags.filter(function(n) {
return this.has(n.name);
}, new Set(active.map(n => n.name)));
const tagsObj = tags.reduce((acc, n) => (acc[n.name] = n, acc), {});
const result = active.reduce((acc, n) => ((n = tagsObj[n.name]) && acc.push(n), acc), []);