const count1 = [
num => (('' + num).match(/1/g) || []).length,
num => num.toString().replace(/[^1]/g, '').length,
num => `${num}`.split('').filter(d => !~-d).length,
num => [...String(num)].reduce((s, d) => s + (d == 1), 0),
num => ''.split.call(num, 1).length - 1,
];
const numbers = [
23489,
-11,
-93481,
7211231,
0,
123.321,
Infinity,
NaN,
];
count1.map(f => numbers.filter(n => f(n) === 2)).forEach(n => console.log(n));
create: function() { на create: function(e, ui) {.
document.querySelector('.nav__list').addEventListener('click', function(e) {
const sub = e.target.nextElementSibling;
if (sub && sub.classList.contains('nav__sublist')) {
sub.classList.toggle('nav--show');
}
});
for (const k in obj) {
obj[k] = Object.values(obj[k]);
}const newObj = Object
.keys(obj)
.reduce((acc, k) => (acc[k] = Object.values(obj[k]), acc), {});
// или
const newObj = Object.fromEntries(Object
.entries(obj)
.map(n => [ n[0], Object.values(n[1]) ])
);
const ul = document.querySelector('#ul');
const button = document.querySelector('#but');ul.addEventListener('click', function(e) {
if (e.target.tagName === 'LI') {
e.target.textContent += '!';
}
});
button.addEventListener('click', function() {
const li = document.createElement('li');
li.textContent = `пункт ${ul.children.length + 1}`;
ul.appendChild(li);
});ul.addEventListener('click', ({ target: t }) => t.matches('li') && t.append('!'));
button.addEventListener('click', () => {
ul.insertAdjacentHTML('beforeend', `<li>пункт ${-~ul.children.length}</li>`);
});
async function processData(data, delay, chunkSize, process) {
let i = -1;
let j = -1;
for (const n of data) {
if (++j === chunkSize) {
j = 0;
await new Promise(r => setTimeout(r, delay));
}
process(n, ++i);
}
}processData(Array(10).keys(), 1500, 3, console.log).then(() => console.log('DONE'));
processData('ABCDEFGHIJKL', 1200, 5, console.log).then(() => console.log('DONE'));
processData(document.images, 900, 7, n => console.log(n.src)).then(() => console.log('DONE'));
for (const [ index, el ] of Object.entries(elems)) {
...for (const [ index, el ] of Array.prototype.entries.call(elems)) {
...const elems = [...document.getElementsByClassName('one')];
for (const [ index, el ] of elems.entries()) {
...const elems = document.querySelectorAll('.one');
for (const [ index, el ] of elems.entries()) {
...
Object.values(names.reduce((acc, n) => {
const g = /^\d/.test(n[0]) ? n[0] : /^[А-ЯЁ]/i.test(n[0]) ? 'А-Я' : null;
g && (acc[g] = acc[g] || [ g ]).push(n);
return acc;
}, {}))
<div class="items"></div>.<select>
<option value="*">Все</option>
<option value=".red">Красные</option>
<option value=".blue">Синие</option>
<option value=".green">Зеленые</option>
</select>$('select').change(function() {
const selector = this.value;
const $items = $('.items >');
$items.filter(selector).slideDown();
$items.not(selector).slideUp();
});
const compareArrays = (a, b) => a.length === b.length && a.every((n, i) => n === b[i]);
+str.replace(/[^\d.]/g, '')parseFloat(str.match(/[\d.]/g).join(''))
arr.reduceRight((_, n, i, a) => indexes.includes(i) && a.splice(i, 1), null);
// или
[...indexes].sort((a, b) => b - a).forEach(i => arr.splice(i, 1));
// или
arr.splice(0, arr.length, ...arr.filter((n, i) => indexes.indexOf(i) === -1));const newArr = arr.filter(((indexes, n, i) => !indexes.has(i)).bind(null, new Set(indexes)));
function createRandomArr(length, min, max) {
if (length > (max -= ~-min)) {
throw 'невозможно создать массив указанного размера';
}
const values = new Set;
for (; values.size < length; values.add(min + Math.random() * max | 0)) ;
return [...values];
}
const arr = createRandomArr(6, 1, 36);const createRandomArr = (length, min, max) => Array
.from({ length }, function() {
return this.splice(Math.random() * this.length | 0, 1);
}, Array.from({ length: max - min + 1 }, (_, i) => i + min))
.flat();function createRandomArr(length, min, max) {
const arr = Array.from({ length: max - min + 1 }, (_, i) => i + min);
for (let i = arr.length; --i > 0;) {
const j = Math.floor(Math.random() * (i + 1));
[ arr[j], arr[i] ] = [ arr[i], arr[j] ];
}
return arr.slice(-length);
}