используется для получения и установки инлайновых стилей
const result = arr
.reduce((acc, n, i, a) => (
a[i - 1] !== n && acc.push([ n, 0 ]),
acc[acc.length - 1][1]++,
acc
), [])
.map(([ v, c ]) => c === 1 ? v : Array(c).fill(v));
const result = arr
.reduce((acc, n, i, a) => (
a[i - 1] === n || acc.push([]),
acc[acc.length - 1].push(n),
acc
), [])
.map(n => n.length === 1 ? n[0] : n);
const result = arr.reduce((acc, n, i, a) => {
const prev = n === a[i - 1];
const next = n === a[i + 1];
!prev && next && acc.push([]);
(prev || next ? acc[acc.length - 1] : acc).push(n);
return acc;
}, []);
const length = 48;
const step = 30;
const locale = 'en-US';
const options = {
hour: '2-digit',
minute: '2-digit',
};
const times = Array.from({ length }, (_, i) => {
return new Date(0, 0, 0, 0, step * i).toLocaleTimeString(locale, options);
});
// или
const times = Array.from({ length }, function() {
return this[1].format(this[0].setMinutes(this[0].getMinutes() + step));
}, [ new Date(0, 0, 0, 0, -step), new Intl.DateTimeFormat(locale, options) ]);
[...`${num}`].map(Number)
num.toString().split('').map(n => +n)
Array.from(String(num), parseFloat)
('' + num).match(/./g).map(n => parseInt(n))
Object.values(num.toFixed()).map(n => ~~n)
[].map.call(/.+/.exec(num)[0], n => n * 1)
eval('['.concat(num, ']').replace(/\d/g, '$&,'))
Object.assign([], JSON.stringify(num)).map(JSON.parse)
Array(1 + (Math.log10(num) | 0)).fill().map((n, i) => (num / 10 ** i | 0) % 10).reverse()
((f = (x, a) => (a.unshift(x % 10), x = x / 10 | 0, x ? f(x, a) : a)) => f(num, []))()
const data = {
[name]: value,
};
const data = Object.fromEntries([ [ name, value ] ]);
<table>
<tr>
<td><div class="draggable">0</div></td>
<td></td>
<td></td>
</tr>
<tr>
<td></td>
<td><div class="draggable">1</div></td>
<td><div class="draggable">2</div></td>
</tr>
<tr>
<td></td>
<td><div class="draggable">3</div></td>
<td></td>
</tr>
</table>
td {
border: 1px solid silver;
width: 100px;
height: 100px;
}
.draggable {
display: inline-block;
width: 25px;
height: 25px;
background: red;
cursor: pointer;
margin: 5px;
color: white;
}
$('.draggable').draggable({
containment: 'table',
stop(e, ui) {
$(this).css({
left: '',
top: '',
});
},
});
$('td').droppable({
accept: '.draggable',
drop(e, ui) {
ui.draggable.appendTo(this);
},
});
const viewBox = Array
.from(document.querySelectorAll('svg'), n => n.getAttribute('viewBox').split(' '))
.reduce((acc, n) => (n.forEach((m, i) => (acc[i] = (acc[i] ?? 0) + +m)), acc), [])
.join(' ');
.spoiler_block div {
display: none;
}
$('.spoiler_title').click(function() {
$('.spoiler_block div').slideToggle(250);
});
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 tbody 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
const last = document.querySelector('#container').lastElementChild;
остальные удалить
Array.prototype.reduceRight.call(
document.getElementById('container').children,
(_, n) => n?.nextElementSibling && n.replaceWith(),
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]);
}
}
}