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);
}
что ещё запрещено делать в content script?
function uniqueWithSum(arr, key, sumKey) {
const getKey = key instanceof Function ? key : n => n[key];
return Object.values(arr.reduce((acc, n) => {
const k = getKey(n);
acc[k] = acc[k] || Object.assign(n.constructor(), n, { [sumKey]: 0 });
acc[k][sumKey] += n[sumKey];
return acc;
}, {}));
}// ваш случай
const result = uniqueWithSum(arr, n => n.id, 'duration');
// элементам не обязательно быть объектами, это могут быть и массивы
uniqueWithSum([
[ 'A', 1 ],
[ 'B', 5 ],
[ 'A', 2 ],
[ 'A', 3 ],
], 0, 1) // [ [ 'A', 6 ], [ 'B', 5 ] ]
<script src="//cdnjs.cloudflare.com/ajax/libs/validate.js/0.12.0/validate.min.js"></script>
[...document.querySelectorAll('.table__checkbox input')].some(input => input.checked)const randomDelay = () => new Promise(resolve => setTimeout(resolve, Math.random()*1000));
(async function() {
let counter = 0;
while (true) {
console.time('timer');
await randomDelay();
console.timeEnd('timer');
if (counter++ > 20) {
break;
}
}
})();
people.filter((n, i, a) => n.country && i === a.findIndex(m => m.country === n.country))people.filter(n => n.country && n === people.find(m => m.country === n.country))people.filter(function({ country: n }) {
return n && !(this[n] = this.hasOwnProperty(n));
}, {})Object.values(people.reduce((acc, n) => (n.country && (acc[n.country] = n), acc), {}))[...people.reduce((acc, n) => n.country ? acc.set(n.country, n) : acc, new Map).values()]Array.from(
new Set(people.map(n => n.country).filter(Boolean)),
n => people.find(m => m.country === n)
)
Вопрос - правильный ли такой подход?