function compoundMatch(words, target) {
const pairs = [];
for (let i = 1; i < target.length; i++) {
pairs.push([ target.slice(0, i), target.slice(i) ]);
}
for (const [ a, b ] of pairs) {
const ia = words.indexOf(a);
const ib = words.indexOf(b);
if (ia !== -1 && ib !== -1) {
return ia < ib ? [ a, b, [ ia, ib ] ] : [ b, a, [ ia, ib ] ];
}
}
return null;
}
function compoundMatch(words, target) {
const indices = {};
for (let i = 0; i < words.length; i++) {
const a = words[i];
if (!indices.hasOwnProperty(a)) {
indices[a] = i;
const b = target.replace(a, '');
if (indices.hasOwnProperty(b)) {
return [ b, a, a + b === target ? [ i, indices[b] ] : [ indices[b], i ] ];
}
}
}
return null;
}
function compoundMatch(words, target) {
const indices = {};
for (let i = 0; i < words.length; i++) {
indices[words[i]] = i;
}
for (const a in indices) {
for (const b in indices) {
if (a + b === target) {
return indices[a] < indices[b]
? [ a, b, [ indices[a], indices[b] ] ]
: [ b, a, [ indices[a], indices[b] ] ];
}
}
}
return null;
}
.active {
background: black;
color: white;
}
const handler = e =>
(e.type === 'click' || e.buttons === 1) &&
e.target.classList.contains('but') &&
e.target.classList.add('active');
document.addEventListener('click', handler);
document.addEventListener('mousemove', handler);
const iKey = 0;
const iTarget = 2;
const values = {
'если есть такое значение': 'подставляем это',
'а при наличие такого': 'ну вы поняли, что здесь должно быть',
// ну и так далее
};
$('.table tr').each(function() {
const $td = $('td', this);
const key = $td.eq(iKey).text();
if (values.hasOwnProperty(key)) {
$td.eq(iTarget).text(values[key]);
}
});
// или
for (const { rows } of document.querySelector('.table').tBodies) {
for (const { cells: { [iKey]: k, [iTarget]: t } } of rows) {
t.textContent = values[k.textContent] || t.textContent;
}
}
<div class="icon"></div>
.icon {
font-size: 100px;
border: 0.1em solid black;
border-radius: 50%;
display: inline-block;
width: 1em;
height: 1em;
position: relative;
transform: rotate(45deg);
}
.icon::before {
content: "";
display: inline-block;
position: absolute;
background: black;
width: 0.1em;
height: 100%;
left: 0.5em;
transform: translateX(-50%);
}
найти последний div
const last = document.querySelector('#container').lastElementChild;
остальные удалить
Array.prototype.reduceRight.call(
document.getElementById('container').children,
(_, n) => n?.nextElementSibling && (n.outerHTML = ''),
null
);
const last = Array
.from(document.querySelectorAll('[id="container"] > *'))
.reduce((_, n, i, a) => i === ~-a.length ? n : n.remove(), null);
const table = document.querySelector('table');
const className = 'red';
const indices = Array.from(table.querySelectorAll(`thead .${className}`), n => n.cellIndex);
table.querySelectorAll('tbody tr').forEach(n => {
indices.forEach(i => n.cells[i].classList.add(className));
});
for (const { rows } of table.tBodies) {
for (const { cells } of rows) {
for (let i = 0, j = 0; i < cells.length; i++) {
j += cells[i].classList.toggle(className, i === indices[j]);
}
}
}
document.addEventListener('click', ({ target: t }) => {
if (t.classList.contains('box')) {
for (const n of document.querySelectorAll('.box')) {
n.style.background = n === t ? 'red' : 'black';
}
}
});
Меняет только value в input без изменения модели
v-model
слушает событие input, так что добавьте event.target.dispatchEvent(new Event('input'))
function getDatesGroupedByWeekday(year, month) {
const d = new Date(`${month} 1, ${year}`);
const iMonth = d.getMonth();
const result = {};
while (d.getMonth() === iMonth) {
const date = d.getDate();
const weekday = d.toLocaleString('en-US', { weekday: 'long' });
(result[weekday] = result[weekday] ?? []).push(date);
d.setDate(date + 1);
}
return result;
}
пишет что state is equal
basketItems: state.basketItems.sort((a, b) => a.price - b.price),