const count = arr.reduce((acc, n) => (acc[n] = (acc[n] ?? 0) + 1, acc), {});
const newArr = arr.map(n => count[n] > 1 ? newValue : n);
// или
const newArr = arr.map(function(n) {
return this.get(n) ? newValue : n;
}, arr.reduce((acc, n) => acc.set(n, acc.has(n)), new Map));
// или
const newArr = arr.map((n, i, a) => a.indexOf(n) !== a.lastIndexOf(n) ? newValue : n);
arr.forEach(function(n, i, a) {
a[i] = this.get(n) > 1 ? newValue : n;
}, arr.reduce((acc, n) => acc.set(n, -~acc.get(n)), new Map));
// или
const duplicates = arr.reduce((acc, n) => (acc[n] = acc.hasOwnProperty(n), acc), {});
arr.splice(0, arr.length, ...arr.map(n => duplicates[n] ? newValue : n));
// или
Object
.values(arr.reduce((acc, n, i) => ((acc[n] ??= []).push(i), acc), {}))
.forEach(n => n.length > 1 && n.forEach(i => arr[i] = newValue));
$('.payment-innovation .item').css('opacity', 0);
$('.payment-innovation .list-block__head').click(function() {
const $this = $(this);
const $items = $this.closest('.list-block').find('.item');
const visible = $this.toggleClass('visible').hasClass('visible');
const duration = 400;
$items.each((i, n) => $(n)
.delay((visible ? i : $items.length - i - 1) * duration)
.animate({ opacity: +visible }, duration)
);
}).click();
const index = arr.findIndex(n => n.id === newObj.id);
if (index !== -1) {
arr[index] = newObj;
} else {
arr.push(newObj);
}
const obj = arr.find(n => n.id === newObj.id);
if (obj) {
Object.assign(obj, newObj);
} else {
arr.push(newObj);
}
stuff.map(n => `${n.firstName} ${n.lastName}`).sort((a, b) => a.localeCompare(b))
$('селектор родительских элементов инпутов')
.last()
.clone()
.find('input')
.attr('name', (i, name) => name.replace(/(?<=\[)\d+(?=\])/, m => +m + 1))
.end()
.appendTo('кому-то там добавляется, сами разберётесь кому');
На IOS не работает (safari)
name.replace(/\[(\d+)\]/, (m, g1) => `[${+g1 + 1}]`)
const groupedPosts = Object.values(data.reduce((acc, n) => (
(acc[n.id] ??= []).push(n),
acc
), {}));
const button = document.querySelector('button');
button.addEventListener('click', showPosts);
const SHOW_POSTS_GROUPS = 2;
for (let i = 0; i < SHOW_POSTS_GROUPS; i++) {
showPosts();
}
function showPosts() {
const posts = groupedPosts.shift();
if (posts) {
const html = `<ul>${posts.map(n => `<li>${n.name}</li>`).join('')}</ul>`;
document.querySelector('.card__wrap').insertAdjacentHTML('beforeend', html);
button.disabled = !groupedPosts.length;
}
}
const interval = [ 0, 150 ];
const $slider = $('#storlekslider').slider({
range: 'max',
min: interval[0],
max: interval[1],
step: 1,
slide: (e, ui) => update(ui.value),
});
const $input = $('#storlek_testet').on('input', e => update(e.target.value));
$('button').click(e => update(+$input.val() + 50 * ($(e.target).text() === '+' ? 1 : -1)));
function update(val) {
val = Math.max(interval[0], Math.min(interval[1], val));
$input.val(val);
$slider.slider('value', val).find('.ui-slider-handle').text(val);
}
const processNonEmptyObjectOnly = (f, defaultResult) =>
data => (
data = data instanceof Object ? Object.entries(data) : [],
data.length ? f(data) : defaultResult
);
const createTreeHTML = processNonEmptyObjectOnly(data => `
<ul>${data.map(n => `
<li>
${n[0]}
${createTreeHTML(n[1])}
</li>`).join('')}
</ul>`
, '');
document.body.insertAdjacentHTML('beforeend', createTreeHTML(data));
const createTreeElement = processNonEmptyObjectOnly(data =>
data.reduce((ul, n) => (
ul.append(document.createElement('li')),
ul.lastChild.append(n[0], createTreeElement(n[1])),
ul
), document.createElement('ul'))
, '');
document.body.append(createTreeElement(data));
Note: This API has been removed in jQuery 3.0; please use.on( "load", handler )
instead of.load( handler )
and.trigger( "load" )
instead of.load()
const grouped = Object.entries(arr.reduce((acc, n) => (
(acc[n.id] ??= []).push(n.name),
acc
), {}));
document.body.insertAdjacentHTML('beforeend', grouped
.map(([ k, v ]) => `
<ul id="list-${k}">${v.map(n => `
<li>${n}</li>`).join('')}
</ul>`)
.join('')
);
document.body.append(...grouped.map(([ id, names ]) => {
const ul = document.createElement('ul');
ul.id = `list-${id}`;
ul.append(...names.map(n => {
const li = document.createElement('li');
li.textContent = n;
return li;
}));
return ul;
}));
str.replace(/\d{3}-\d{2}/, '***-**')
// или
str.slice(0, 9) + '***-**' + str.slice(15)
// или
str.replace(/\d(?=\d*-)/g, '*')
// или
str.replace(/\d+(?=-)/g, m => '*'.repeat(m.length))
// или
str.replace(/(?<=\) ).{6}/, '***-**')
// или
str.match(/^.{9}|.{3}$/g).join('***-**')