document.querySelectorAll('h2').forEach(n => {
const [ season, , episode ] = n.innerText.split(' ');
if (+episode === 1) {
n.id = `season-${season}`;
}
});
[...document.querySelectorAll('h2')]
.filter(n => n.innerText.endsWith(', 1 серия'))
.forEach((n, i) => n.id = `season-${i + 1}`);
document.querySelectorAll('h2').forEach((n, i, a) => {
const prev = i && a[i - 1].innerText.match(/\d+/)[0];
const curr = n.innerText.match(/\d+/)[0];
if (curr !== prev) {
n.id = `season-${curr}`;
}
});
Array
.from(document.querySelectorAll('h2'))
.reduce((acc, n) => (acc[parseInt(n.innerText)] ??= n, acc), [])
.forEach((n, i) => n.id = `season-${i}`);
function rotateArray($arr, $shift) {
$shift %= count($arr);
array_unshift($arr, ...array_splice($arr, -$shift));
return $arr;
}
$arr = range(1, 7);
echo implode(', ', rotateArray($arr, 1)); // 7, 1, 2, 3, 4, 5, 6
echo implode(', ', rotateArray($arr, -3)); // 4, 5, 6, 7, 1, 2, 3
echo implode(', ', rotateArray($arr, 69)); // 2, 3, 4, 5, 6, 7, 1
<div class="slider">
$('.slick').slick({
data-category="orange"
var filterClass = $(this).data('category');
$('.slick').slick('slickFilter', filterClass);
$('.category__menu').on('click', 'li', function() {
$('.slider').slick('slickUnfilter');
const category = $(this).data('category');
if (category !== 'allPost') {
$('.slider').slick('slickFilter', `.${category}`);
}
});
const transpose = matrix => Array.from(
{ length: matrix[0]?.length ?? 0 },
(_, i) => matrix.map(n => n[i])
);
function transpose(matrix) {
const result = Array(matrix.length && matrix[0].length);
for (let i = 0; i < result.length; i++) {
result[i] = [];
for (let j = 0; j < matrix.length; j++) {
result[i][j] = matrix[j][i];
}
}
return result;
}
const groupAdjacent = (arr, newGroup) =>
arr.reduce((acc, n, i, a) => (
(!i || newGroup(n, a[~-i])) && acc.push([]),
acc[~-acc.length].push(n),
acc
), []);
const result = groupAdjacent(arr, (c, p) => c !== -~p);
function group(data, key) {
const grouped = new Map;
let i = -1;
for (const n of data) {
const k = key(n, ++i);
grouped.set(k, grouped.get(k) ?? []).get(k).push(n);
}
return grouped;
}
const result = [...group(arr, (n, i) => n - i).values()];
const [ form, setForm ] = useState({
login: '',
password: '',
});
const onChange = ({ target: t }) => setForm({ ...form, [t.name]: t.value });
<input name="login" value={form.login} onChange={onChange} />
<input name="password" value={form.password} onChange={onChange} />
$('.js-cropped-word').text((i, text) => text.replace(/(?<=\S{19,}).+/, '...'));
В Сафари не работает
document.querySelectorAll('.js-cropped-word').forEach(n => {
n.textContent = n.textContent.replace(/(\S{19}).+/, '$1...');
});
const max = 19;
for (const n of document.getElementsByClassName('js-cropped-word')) {
const words = n.innerText.split(' ');
const i = words.findIndex(n => n.length > max);
if (i !== -1) {
words.length = i + 1;
words[i] = words[i].slice(0, max) + '...';
n.innerText = words.join(' ');
}
}
const elements = document.querySelectorAll('input[type="button"]');
const wrapperTag = 'div';
const wrapperClass = 'block';
elements.forEach(n => {
const wrapper = document.createElement(wrapperTag);
n.replaceWith(wrapper);
wrapper.classList.add(wrapperClass);
wrapper.append(n);
});
for (const n of elements) {
n.after(document.createElement(wrapperTag));
n.nextSibling.className = wrapperClass;
n.nextSibling.appendChild(n);
}
for (let i = 0; i < elements.length; i++) {
const n = elements[i];
n.outerHTML = `<${wrapperTag} class="${wrapperClass}">${n.outerHTML}</${wrapperTag}>`;
}
(function wrap(i, n = elements[i]) {
if (n) {
const wrapper = document.createElement(wrapperTag);
n.parentNode.replaceChild(wrapper, n);
wrapper.classList.value = wrapperClass;
wrapper.insertAdjacentElement('afterbegin', n);
wrap(-~i);
}
})(0);
const values = [ '1', '2', '3' ];
[...selectEl].forEach(n => values.includes(n.value) && n.remove());
for (const n of selectEl.querySelectorAll(values.map(n => `[value="${n}"]`))) {
selectEl.removeChild(n);
}
for (let i = selectEl.options.length; i--;) {
const n = selectEl.options[i];
if (values.indexOf(n.value) !== -1) {
n.outerHTML = '';
}
}
Обычный img у меня не сработал.
product.amount = 1;
на Vue.set(product, 'amount', 1);
.product.amount = 1;
state.items.push(product);
state.items.push({
...product,
amount: 1,
});
const filterArrByObj = (arr, obj) =>
Object.entries(obj).reduce((arr, [ k, v ]) => {
return arr.filter(Array.isArray(v)
? n => v.includes(n[k])
// !v.length || v.includes(n[k]), если при пустом массиве допустимо любое значение
: n => v === n[k]
// n[k].includes(v), если точное соответствие не нужно
);
}, arr);
computed: {
filteredData() {
return filterArrByObj(this.data, this.filters);
},
},
<tr v-for="n in filteredData">
...
calc__price
и calc__payment
заменить на общий calc
.const $price = $('#price');
const $firstPayment = $('#first-payment');
function update($elem) {
$elem.closest('.calc').find('output').text($elem.val());
$('.payment-percent').html(Math.round($firstPayment.val() * 100 / $price.val()) + '%');
}
$price.closest('.calc').on('input', function() {
const val = $price.val();
$firstPayment
.val((i, v) => Math.min(v, val))
.attr('max', val)
.rangeslider('update', true);
update($firstPayment);
});
$price.add($firstPayment)
.rangeslider({ polyfill: false })
.closest('.calc')
.on('input', e => update($(e.target)))
.end()
.trigger('input');
function walkNested(val, callback) {
callback(val);
if (val instanceof Object) {
Object.values(val).forEach(n => walkNested(n, callback));
}
}
walkNested(obj, n => n?.touched === true && (n.touched = false));
function walkNested(val, callback) {
for (const stack = [ val ]; stack.length;) {
const n = stack.pop();
callback(n);
stack.push(...Object.values(n ?? {}));
}
}
const keys = [ 'city', 'country' ];
.const newArr = arr.map(n => Object.fromEntries(Object.values(n).map((v, i) => [ keys[i], v ])));
arr.forEach(n => Object.entries(n).forEach(([ k, v ], i) => (delete n[k], n[keys[i]] = v)));
str.replace(/\t.*/s, '')
// или
str.match(/^[^\t]*/)[0]
// или
str.slice(0, str.search(/\t|$/))
// или
str.split('\t', 1).pop()
// или
[...str].reduceRight((acc, n) => n === '\t' ? '' : n + acc, '')