arr.sort((a, b) => (a.rooms - b.rooms) || (a.square - b.square));
// или, в более общем виде
const sort = (arr, ...keys) =>
arr.sort((a, b) => {
let diff = 0;
keys.find(k => diff = a[k] - b[k]);
return diff;
});
sort(arr, 'rooms', 'square');
const sorted = (arr, keys) => arr
.map(n => [ n ].concat(keys(n)))
.sort((a, b) => {
let diff = 0;
for (let i = 0; ++i < a.length && !(diff = a[i] - b[i]);) ;
return diff;
})
.map(n => n[0]);
const sortedArr = sorted(arr, n => [ n.rooms, n.square ]);
const $table = $('table');
const columnIndex = // здесь должен быть номер столбца с числами
const sum = $table
.find('tr:visible')
.get()
.reduce((acc, n) => acc + +$('td', n).eq(columnIndex).text(), 0);
$table
.find('th')
.eq(columnIndex)
.text(`Вес (${sum})`);
$('.news-block-2').each((i, n) => $('.text-news', n).after($('.picture-news', n)));
$('.news-block-2 tr').append(function() {
return $('.picture-news', this);
});
$('.news-block-2 .text-news').each((i, n) => $(n).parent().prepend(n));
$('.news-block-2 .picture-news').before(function() {
return $(this).next();
});
X = n => n ? (n & 1 ? '-chirp' : '') + X(n >> 1) + X(n >> 1) : ''
chirp = n => X(n).slice(1)
google.visualization.arrayToDataTable(data, true)
Array
.from(document.querySelectorAll('.obj'))
.filter(n => n.querySelector('.who')?.innerText !== 'Я')
.forEach(n => console.log(n.innerText));
const $items = $('.item');
const SLICE_SIZE = 3;
$('.container').append(i => $items.slice(i * SLICE_SIZE, (i + 1) * SLICE_SIZE));
$('label > input').change(function() {
$(this).parent().toggleClass('active', this.checked);
});
const containerSelector = '.card_options > div';
const itemSelector = '.option';
const activeClass = 'checked';
const toggleItem = item => item
?.closest(containerSelector)
.querySelectorAll(itemSelector)
.forEach(n => n.classList[n === item ? 'toggle' : 'remove'](activeClass));
// можно добавить обработчик клика каждому элементу индивидуально
document.querySelectorAll(itemSelector).forEach(function(n) {
n.addEventListener('click', this);
}, e => toggleItem(e.currentTarget));
// а можно один раз - делегированный, всей странице
document.addEventListener('click', e => {
toggleItem(e.target.closest(itemSelector));
});
const isArithmeticProgression = arr =>
arr.length > 1
? new Set(arr.map((n, i, a) => n - a[i + 1]).slice(0, -1)).size === 1
: !!arr.length;
// или
const isArithmeticProgression = arr =>
!!arr.length && arr.every((n, i, a) => !i || (n - a[i - 1] === a[1] - a[0]));
const arr = Object.values(obj);
. Дальше есть варианты:const result = arr.sort((a, b) => a.position - b.position).map(n => n.color);
const result = arr
.reduce((acc, n) => ((acc[n.position] = acc[n.position] ?? []).push(n.color), acc), [])
.flat();
const result = Object.values(Object.fromEntries(arr.map(n => [ n.position, n.color ])));