const mustStay = arr => arr.some(n => n !== '');
const newArr = arr.filter(mustStay);
arr.reduceRight((_, n, i, a) => mustStay(n) || a.splice(i, 1), null);
// или
arr.splice(0, arr.length, ...arr.filter(mustStay));
// или
let numDeleted = 0;
for (const [ i, n ] of arr.entries()) {
arr[i - numDeleted] = n;
numDeleted += !mustStay(n);
}
arr.length -= numDeleted;
[...divs.keys()].forEach(i => console.log(i));
// или
console.log(Array.from(divs.keys(), i => i * 100));
// или
for (const i of divs.keys()) {
console.log(i);
}
const attrName = 'data-i';
const attrValues = [ 'qwe', 'asd', 'zxc' ];
const className = 'класс';
document
.querySelectorAll(attrValues.map(n => `[${attrName}="${n}"]`))
.forEach(n => n.classList.add(className));
for (const n of document.querySelectorAll(`[${attrName}]`)) {
n.classList.toggle(className, attrValues.includes(n.getAttribute(attrName)));
}
на JS...
document.querySelectorAll('.scroll_list li').forEach(n => {
n.innerHTML = n.textContent.replace(/\d+ шт./, '<span>$&</span>');
});
// или
for (const n of document.querySelector('.scroll_list').children) {
n.innerHTML = n.innerText.replace(/\S+ \S+$/, m => `<span>${m}</span>`);
}
...или PHP
span
создавался сразу. <p>
<b>Text</b>
<input type="text" id="text">
<b>Color</b>
<input type="text" id="color">
</p>
<button id="create">create</button>
<button id="update">update</button>
<button id="del">delete</button>
<ul id="items"></ul>
.active {
border: 2px solid black;
}
const
[ items, text, color, create, update, del ] =
[ 'items', 'text', 'color', 'create', 'update', 'del' ]
.map(n => document.getElementById(n));
create.addEventListener('click', () => {
items.insertAdjacentHTML('beforeend', `<li style="color: ${color.value}">${text.value}</li>`);
updateForm('', '');
});
update.addEventListener('click', () => ifActive(a => {
a.innerText = text.value;
a.style.color = color.value;
}));
del.addEventListener('click', () => ifActive(a => a.remove()));
items.addEventListener('click', e => {
const item = e.target.closest('li');
if (item) {
ifActive(a => item !== a && a.classList.remove('active'));
item.classList.toggle('active');
ifActive(a => updateForm(a.innerText, a.style.color));
}
});
function ifActive(f) {
const active = items.querySelector('.active');
active && f(active);
}
function updateForm(textVal, colorVal) {
text.value = textVal;
color.value = colorVal;
}
nums1 = nums1.slice(0, m).concat(nums2).sort((a, b) => a - b);
nums1.splice(0, nums1.length, ...[ ...nums1.slice(0, m), ...nums2 ].sort((a, b) => a - b));
// или
nums1.slice(0, m).concat(nums2).sort((a, b) => a - b).forEach((n, i) => nums1[i] = n);
const ceil = (value, precision) => Math.ceil(value / precision) * precision;
const values = [ 1, 2, 3, 163, 200.001, 99.999 ];
values.map(n => ceil(n, 1)); // [ 1, 2, 3, 163, 201, 100 ]
values.map(n => ceil(n, 10)); // [ 10, 10, 10, 170, 210, 100 ]
values.map(n => ceil(n, 5)); // [ 5, 5, 5, 165, 205, 100 ]
<div class="angle">
<div class="angle-input">
<div class="angle-input-arrow"></div>
</div>
<input type="number" min="0" max="360" value="0">
</div>
.angle {
display: inline-flex;
align-items: center;
border: 1px solid silver;
padding: 5px;
}
.angle-input {
display: inline-flex;
justify-content: center;
border: 1px solid black;
border-radius: 50%;
width: 50px;
height: 50px;
margin-right: 10px;
}
.angle-input-arrow {
height: 50%;
border: 1px solid black;
transform-origin: 50% 100%;
box-sizing: border-box;
pointer-events: none;
}
document.querySelector('.angle input').addEventListener('input', function(e) {
const arrow = this.closest('.angle').querySelector('.angle-input-arrow');
arrow.style.transform = `rotate(${e.target.value}deg)`;
});
const angleInput = document.querySelector('.angle-input');
angleInput.addEventListener('mousedown', onAngleInput);
angleInput.addEventListener('mousemove', onAngleInput);
function onAngleInput(e) {
if (e.buttons === 1) {
const {
offsetX: x, offsetY: y,
target: t,
target: { offsetWidth: w, offsetHeight: h }
} = e;
const deg = (450 + (Math.atan2(y - h / 2, x - w / 2) * (180 / Math.PI) | 0)) % 360;
t.querySelector('.angle-input-arrow').style = `transform: rotate(${deg}deg)`;
t.closest('.angle').querySelector('input').value = deg;
}
}
function chunked(arr, numChunks) {
const chunks = [];
const chunkSize = arr.length / numChunks | 0;
const numLooseItems = arr.length % numChunks;
for (let i = 0, j = 0; j < numChunks; j++) {
chunks.push(arr.slice(i, i += chunkSize + (j < numLooseItems)));
}
return chunks;
}
const [ head, tail ] = chunked(arr, 2);
const itemSelector = '.parent';
const className = 'xxx';
document.querySelectorAll(itemSelector).forEach(n => {
n.addEventListener('mouseenter', onHover);
n.addEventListener('mouseleave', onHover);
});
function onHover(e) {
const state = e.type === 'mouseenter';
for (
let el = this;
(el = el.nextElementSibling) && !el.matches(itemSelector);
el.classList.toggle(className, state)
) ;
}
.parent
, чтобы создать видимость, будто бы стили не применялись:.parent:hover ~ .child {
...
}
.parent:hover ~ .parent ~ .child {
...
}
document.querySelectorAll('.selectit > input').forEach(n => {
n.checked = genres.includes(n.parentNode.innerText.trim());
});
for (const n of document.getElementsByClassName('selectit')) {
n.children[0].checked = genres.indexOf(n.textContent.trim()) > -1;
}
ind.map(((data, n) => data[n]).bind(null, arr.reduce((acc, row) => {
const d = acc[row[0]];
row.forEach((n, i) => i && (d[i] = d[i] || []).push(n));
return acc;
}, ind.reduce((acc, n) => (acc[n] = [ n ], acc), {}))))
array.reduce((acc, el) => {
el.forEach((n, i) => {
acc[i] = acc[i] || [];
n.forEach((m, j) => (acc[i][j] = acc[i][j] || []).push(...[].concat(m)));
});
return acc;
}, [])
const result = ind.reduce((acc, col) => {
arr.forEach((n, i) => {
acc[i].push(i ? n.filter((m, j) => arr[0][j] === col) : col);
});
return acc;
}, arr.map(() => []));
const indObj = Object.fromEntries(ind.map((n, i) => [ n, i ]));
const result = arr.reduce((acc, n, i) => (
i && n.forEach((m, j) => acc[i][indObj[arr[0][j]]].push(m)),
acc
), arr.map((_, i) => ind.map(n => i ? [] : n)));
const min = 666;
const inputSelector = 'input';
const buttonSelector = 'button';
const input = document.querySelector(inputSelector);
const button = document.querySelector(buttonSelector);
input.addEventListener('input', e => button.disabled = e.target.value < min);
input.dispatchEvent(new Event('input'));
// или
const $input = $(inputSelector);
const $button = $(buttonSelector);
$input.on('input', () => $button.prop('disabled', $input.val() < min)).trigger('input');