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);
}
async function request(data) {
let result = data;
do {
result = await запрос(result);
} while (result не тот, который нужен);
return result;
}function requestRecursive(data) {
return запрос(data).then(result => {
return result тот, который нужен
? result
: requestRecursive(result);
});
}
const values = Object.values(obj);const count = values.flat().length;
// или
const count = [].concat(...values).length;
// или
const count = values.reduce((acc, n) => acc + n.length, 0);
// или
const count = eval(values.map(n => n.length).join('+')) || 0;
a = [ 1, 2, 3 ];
b = [ 4, 5, 6 ];
c = a;
// здесь должен быть какой-то код
console.log(a); // [ 4, 5, 6 ]
console.log(b); // [ 1, 2, 3 ]
console.log(a === c); // trueb.splice(0, b.length, ...a.splice(0, a.length, ...b));
Array.from(new Set(arr.flat()))
// или, без создания промежуточного массива
[...arr.reduce((acc, n) => (n.forEach(m => acc.add(m)), acc), new Set)]Object.keys([].concat(...arr).reduce((acc, n) => (acc[n] = 1, acc), {}))
// или
Array.prototype.concat.apply([], arr).filter((n, i, a) => i === a.indexOf(n))
// или
`${arr}`.split(',').reduce((acc, n) => (acc.includes(n) || acc.push(n), acc), [])
// или
String(arr).match(/\w/g).sort().filter((n, i, a) => n !== a[i - 1])
// или
arr.toString().match(/(\w)(?!.*\1)/g) || []
function someFunction([ ,,, id,, status ]) {function someFunction({ 3: id, 5: status }) {
this внутри обработчика клика? Не тем, что вам кажется. Замените $(this).attr("href") на $(event.target).attr("href") или стрелочную функцию на обычную.
msg.razd массив значений:const arr = Object.values(msg.razd);.const categories = arr.flatMap(n => n.category);
// или
const categories = [].concat.apply([], arr.map(n => n.category));
// или
const categories = arr.reduce((acc, n) => (acc.push(...n.category), acc), []);const result = categories.filter(n => expect.includes(n._id));
// или
const result = categories.filter(function(n) {
return this.has(n._id);
}, new Set(expect));
// или
const categoriesObj = Object.fromEntries(categories.map(n => [ n._id, n ]));
const result = expect.reduce((acc, n) => ((n = categoriesObj[n]) && acc.push(n), acc), []);
setTimeout((current) => pika[current].classList.toggle('active'), 100, current)
const $test = $('.test').on('click', '.testText', function() {
$test.append($(this).clone());
});$('.testText').click(function() {
$('.test').append($(this).clone(true));
});
data-country почему-то на русском - какого чёрта? Надо, чтобы были такими же, как и id у элементов .tabcontent.const tabLinks = document.querySelectorAll('.tablinks');
const tabContent = document.querySelectorAll('.tabcontent');
tabLinks.forEach(n => n.addEventListener('click', openTab));
function openTab({ currentTarget: t }) {
const { country } = t.dataset;
tabLinks.forEach(n => n.classList.toggle('active', n === t));
tabContent.forEach(n => n.classList.toggle('active', n.id === country));
// или, к чёрту data-атрибуты и id, можно индексами воспользоваться;
// конечно, в этом случае необходимо, чтобы .tablinks и соответствующие им
// .tabcontent были расположены в одинаковом порядке
const index = Array.prototype.indexOf.call(tabLinks, t);
tabLinks.forEach(n => n.classList.toggle('active', n === t));
tabContent.forEach((n, i) => n.classList.toggle('active', i === index));
}