function replaceNumbers(str, matches) {
let count = 0;
return str.replace(/\d/g, d => matches.includes(++count) ? '*' : d);
}
replaceNumbers('+7 (000) 000-00-00', [ 2, 3, 4 ]); // "+7 (***) 000-00-00"
replaceNumbers('+7 (000) 000-00-00', [ 2, 3, 4, 5, 6, 7 ]); // "+7 (***) ***-00-00"
replaceNumbers('+7 (000) 000-00-00', [ 10, 11 ]); // "+7 (000) 000-00-**"
{ required: true }
- что вы хотели этим сказать? Непонятно. Вот вам пример валидации вашей формы, а вот ссылка на документацию - тоже пригодится. str.split('/').pop()
//или
str.slice(str.lastIndexOf('/') + 1)
// или
[...str].reduce((acc, n) => n === '/' ? '' : acc + n, '')
str.match(/[^\/]*$/)[0]
// или
str.replace(/.*\//, '')
arr.map((n, i) => n ? i : null).filter(n => n !== null)
// или
arr.map((n, i) => n ? i : NaN).filter(n => n === n)
// или
arr.map((n, i) => !!n && i).filter(Number.isInteger)
// или
arr.map((n, i) => !n || i).filter(n => n !== !0)
arr.reduce((acc, n, i) => n ? [ ...acc, i ] : acc, [])
// или
arr.reduce((acc, n, i) => (n && acc.push(i), acc), [])
for (let i = arr.length; i--;) {
if (arr[i]) {
arr[i] = i;
} else {
arr.splice(i, 1);
}
}
// или
arr.reduceRight((_, n, i, a) => n ? a[i] = i : a.splice(i, 1), 0);
// или
arr.splice(0, arr.length, ...любое_выражение_с_map/filter_или_reduce_из_показанных_выше);
// или
let count0 = 0;
for (const [ i, n ] of arr.entries()) {
arr[i - count0] = i;
count0 += !n;
}
arr.length -= count0;
<button data-move="-10,0">left</button>
<button data-move="10,0">right</button>
<button data-move="0,-10">up</button>
<button data-move="0,10">down</button>
<button data-move="10,10">right down</button>
<button data-move="20,-5">fast right slow up</button>
$(document).on('click', '[data-move]', function() {
const [ dx, dy ] = this.dataset.move.split(',').map(Number);
$('#overlay')
.css('left', (i, val) => `${parseInt(val) + dx}px`)
.css('top', (i, val) => `${parseInt(val) + dy}px`);
});
// или, к чёрту jquery
document.addEventListener('click', e => {
const { move } = e.target.dataset;
if (move) {
const block = document.querySelector('#overlay');
const { left, top } = getComputedStyle(block);
const [ dx, dy ] = move.split(',').map(n => +n);
block.style.left = parseInt(left) + dx + 'px';
block.style.top = parseInt(top) + dy + 'px';
}
});
const $block = $('.block_to_add').clone();
$('.click_to_add_block').click(function() {
$(this).before($block.clone());
});
А подскажите, как удалить один из блоков по клику на другую кнопку?
$(document).on('click', '.delete', function() {
$(this).parent().remove();
});
<button class="delete">удалить</button>
const itemSelector = '.menu-catalog__item';
const buttonSelector = '.menu-catalog__link';
const className = 'menu-catalog__open';
const $items = $(itemSelector).on('click', buttonSelector, e => {
$items.not(e.delegateTarget).removeClass(className);
$(e.delegateTarget).toggleClass(className);
});
// или
const items = document.querySelectorAll(itemSelector);
const onClick = ({ target: t }) =>
t.closest(buttonSelector) && items.forEach(n => {
n.classList[n.contains(t) ? 'toggle' : 'remove'](className);
});
items.forEach(n => n.addEventListener('click', onClick));
function shortNumber(val) {
const numSymbol = [ '', 'K', 'M', 'B', 'T' ];
let symbolIndex = 0;
while (val >= 1000) {
val = Math.round(val / 1000);
symbolIndex++;
}
return val + numSymbol[symbolIndex];
}
shortNumber(96); // 96
shortNumber(22222); // 22K
shortNumber(4951476); // 5M
$('.tabs').on('click', '.tab', function() {
setActiveTab($(this).index());
});
$('.nav').magnificPopup({
type: 'inline',
delegate: 'a',
callbacks: {
open() {
setActiveTab(this.currItem.index);
},
},
});
function setActiveTab(index) {
$('.tab').removeClass('active').eq(index).addClass('active');
$('.tab_item').hide().eq(index).fadeIn();
}
const reverse = str => str.length < 2 ? str : reverse(str.slice(1)) + str[0];
const reverse = ([ c, ...str ]) => c ? reverse(str) + c : '';
$.easing.bullshit = function(x, t, b, c, d) {
return (t == d) ? b + c : c * (-Math.pow(2, -10 * t / d) + 1) + b;
};
$('.ach-number span').each(function() {
$(this).prop('counter', 0).animate({
counter: $(this).text(),
}, {
duration: 10000,
easing: 'bullshit',
step(val) {
$(this).text(Math.ceil(val));
},
});
});
Скажите, может ли он ставить по одной точке и показывать?
const getRandom = (min, max) => Math.floor(Math.random() * (max - min + 1)) + min;
const NUM_POINTS = 100000;
const BASE_POINTS = [
[ 50, 50 ],
[ 1200, 50 ],
[ 625, 500 ],
];
const draw = SVG('drawing').size(1200, 500);
const group = draw.group();
let currPoints = 0;
let point = [ (getRandom(50, 1200)), (getRandom(50, 500)) ];
function nextPoint(previousPoint) {
const basePoint = BASE_POINTS[getRandom(0, BASE_POINTS.length - 1)];
point = [ (previousPoint[0] + basePoint[0]) / 2, (previousPoint[1] + basePoint[1]) / 2 ];
group.add(draw.circle(1).fill('#000').move(point[0], point[1]));
}
(function next() {
if (currPoints < NUM_POINTS) {
for (let i = 0; i < 100; i++, currPoints++) {
nextPoint(point);
}
setTimeout(next, 50);
}
})();
<script src="https://cdnjs.cloudflare.com/ajax/libs/svg.js/2.6.5/svg.min.js"></script>
<div id="drawing"></div>
if (elem == "edit") { app.UserBook.fullView(elem);
fullView(elem) { const d = document; const i = elem; d.querySelector('#Edname').value = this.items[i].fname;
for (let i = 0; i < this.items.length; i++) {