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(new 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)
)
Вопрос - правильный ли такой подход?
use GuzzleHttp\Pool;
use GuzzleHttp\Client;
use GuzzleHttp\Psr7\Request;
$client = new Client();
$requests = function ($total) {
$uri = 'http://127.0.0.1:8126/guzzle-server/perf';
for ($i = 0; $i < $total; $i++) {
yield new Request('GET', $uri);
}
};
$pool = new Pool($client, $requests(100), [
'concurrency' => 5,
'fulfilled' => function ($response, $index) {
// this is delivered each successful response
},
'rejected' => function ($reason, $index) {
// this is delivered each failed request
},
]);
// Initiate the transfers and create a promise
$promise = $pool->promise();
// Force the pool of requests to complete.
$promise->wait();
слышала мнение, что все их знать не надо
Какие используются в геймдеве?
Что значит в вакансиях "знание паттернов проектирования"?
Как проверяют на их знание?