if (item.innerText === currentPage) {item.innerText == currentPage+item.innerText === currentPagedocument.body.insertAdjacentHTML('beforeend', `
<div class="container">
<table>
<thead>
<tr>${keys.map(k => `
<th>${k}</th>`).join('')}
</tr>
</thead>
<tbody></tbody>
</table>
<div class="pagination"></div>
</div>
`);
const tableEl = document.querySelector('.container table');
const paginationEl = document.querySelector('.container .pagination');
paginationEl.addEventListener('click', ({ target: t }) =>
t.matches('a') && showPage(+t.textContent)
);
function showPage(page) {
paginationEl.innerHTML = Array
.from(
{ length: Math.ceil(data.length / rows) },
(_, i) => `<a${-~i === page ? ' class="active"' : ''}>${-~i}</a>`)
.join('');
tableEl.tBodies[0].innerHTML = data
.slice(~-page * rows, page * rows)
.map(n => `
<tr>${keys.map(k => `
<td>${n[k]}</td>`).join('')}
</tr>`)
.join('');
}
showPage(1);
const $inputs = $('.input_1');.Array.from($inputs).forEach(n => console.log(n, n.value ? 'я заполнен' : 'я пуст'));
console.log($inputs.get().some(n => n.value) ? 'кто-то заполнен' : 'все пустые');
console.log($inputs.toArray().every(n => n.value) ? 'все заполнены' : 'кто-то пуст');
console.log(Array.prototype.filter.call($inputs, n => n.value), 'мы заполнены');
console.log([...$inputs.not((i, n) => n.value)], 'мы пустые');
const groupedArr = useMemo(() => {
return Object.values(arr.reduce((acc, { dt, weather }) => {
const [ date, time ] = dt.split(' ');
acc[date] = acc[date] || { date, weather: [] };
acc[date].weather.push({ time, weather });
return acc;
}, {}));
}, [ arr ]);<ul>{groupedArr.map(n => (
<li>
<h3>{n.date}</h3>
<ul>{n.weather.map(m => (
<li>
{m.time} - {m.weather}
</li>))}
</ul>
</li>))}
</ul>
function getDateInTimeZone(utcOffset, date = new Date()) {
const utcTime = date.getTime() + date.getTimezoneOffset() * 60000;
return new Date(utcTime + utcOffset * 3600000);
}
const moscowDate = getDateInTimeZone(3);
const newYorkDate = getDateInTimeZone(-5);
const tokyoDate = getDateInTimeZone(9);
const digital_root = num => num > 9
? digital_root([...`${num}`].reduce((acc, n) => acc + +n, 0))
: num;
почему 14 часов превратились в 02
как этого избежать?
preg_match_all('/\b[а-яё]+\b/ui', $str, $cyrillic);
preg_match_all('/\b[a-z]+\b/i', $str, $latin);
const getNested = (obj, ...keys) => keys
.flat(Infinity)
.flatMap(n => typeof n === 'string' ? n.split('.') : n)
.reduce((p, c) => p?.[c] ?? null, obj);getNested(data, [ 'hosts', 1, null ]) // 3
getNested(data, 'hosts', 0) // {name: 'web1'}
getNested(data, 'hosts.0.name.2') // 'b'
getNested(data, [ 'user', 'xxx' ]) // null
getNested([], 'constructor.prototype', [[['slice']]], ['name.length']) // 5
Object.keys(obj).forEach(n => n !== key && delete obj[n]);obj = { [key]: obj[key] };const deleteKeys = (obj, except) =>
Object.keys(obj).forEach(n => except.includes(n) || delete obj[n]);
const obj = { a: 1, b: 2, c: 3, d: 4 };
deleteKeys(obj, [ 'a' ]);
console.log(obj); // {a: 1}const pick = (obj, keys) =>
Object.fromEntries(keys.map(n => [ n, obj[n] ]));
const obj = { a: 1, b: 2, c: 3, d: 4 };
console.log(pick(obj, [ 'a', 'd' ])); // {a: 1, d: 4}
const className = 'select';
const sumEl = document.querySelector('.sum');
const sum = elements =>
Array.prototype.reduce.call(
elements,
(acc, n) => acc + (+n.value || 0),
0
);const updateSum = () => sumEl.textContent = sum(document.getElementsByClassName(className));
document.addEventListener('change', e => e.target.classList.contains(className) && updateSum());
updateSum();const selects = document.querySelectorAll(`.${className}`);
const updateSum = () => sumEl.innerText = sum(selects);
selects.forEach(n => n.addEventListener('change', updateSum));
updateSum();