const groupQuestions = arr => ({
testId: arr[0].testId,
questions: Object.values(arr.reduce((acc, n) => {
const id = n.questionId;
(acc[id] = acc[id] || {
questionId: id,
questionText: n.questionText,
answers: [],
}).answers.push({
answer: n.answer,
isRight: n.isRight,
});
return acc;
}, {})),
});
const data = [
{
floors: [ 1 ],
limits: [
{ f: 1.26, max: 120 },
{ f: 1.24, max: 140 },
{ f: 1.23, max: 160 },
{ f: 1.22, max: 200 },
{ f: 1.20, max: 260 },
{ f: 1.19, max: 300 },
],
},
{
floors: [ 2, 3 ],
limits: [
{ f: 1.00, max: 100, excludeMax: true },
{ f: 1.30, max: 130 },
{ f: 1.27, max: 160 },
{ f: 1.24, max: 200 },
{ f: 1.22, max: 300 },
],
},
];
const d = data.find(n => n.floors.includes(floors.value));
if (d) {
const v = area.value;
console.log((d.limits.find(n => n.excludeMax ? n.max > v : n.max >= v) || { f: 1 }).f);
}
const count1 = [
num => (('' + num).match(/1/g) || []).length,
num => num.toString().replace(/[^1]/g, '').length,
num => `${num}`.split('').filter(d => !~-d).length,
num => [...String(num)].reduce((s, d) => s + (d == 1), 0),
num => ''.split.call(num, 1).length - 1,
];
const numbers = [
23489,
-11,
-93481,
7211231,
0,
123.321,
Infinity,
NaN,
];
count1.map(f => numbers.filter(n => f(n) === 2)).forEach(n => console.log(n));
create: function() {
на create: function(e, ui) {
. document.querySelector('.nav__list').addEventListener('click', function(e) {
const sub = e.target.nextElementSibling;
if (sub && sub.classList.contains('nav__sublist')) {
sub.classList.toggle('nav--show');
}
});
for (const k in obj) {
obj[k] = Object.values(obj[k]);
}
const newObj = Object
.keys(obj)
.reduce((acc, k) => (acc[k] = Object.values(obj[k]), acc), {});
// или
const newObj = Object.fromEntries(Object
.entries(obj)
.map(n => [ n[0], Object.values(n[1]) ])
);
const ul = document.querySelector('#ul');
const button = document.querySelector('#but');
ul.addEventListener('click', function(e) {
if (e.target.tagName === 'LI') {
e.target.textContent += '!';
}
});
button.addEventListener('click', function() {
const li = document.createElement('li');
li.textContent = `пункт ${ul.children.length + 1}`;
ul.appendChild(li);
});
ul.addEventListener('click', ({ target: t }) => t.matches('li') && t.append('!'));
button.addEventListener('click', () => {
ul.insertAdjacentHTML('beforeend', `<li>пункт ${-~ul.children.length}</li>`);
});
async function processData(data, process, chunkSize, delay) {
let i = -1;
for (const n of data) {
if (++i === chunkSize) {
i = 0;
await new Promise(r => setTimeout(r, delay));
}
process(n);
}
}
processData(Array(10).keys(), console.log, 3, 1500).then(() => console.log('DONE'));
for (const [ index, el ] of Object.entries(elems)) {
...
for (const [ index, el ] of Array.prototype.entries.call(elems)) {
...
const elems = [...document.getElementsByClassName('one')];
for (const [ index, el ] of elems.entries()) {
...
const elems = document.querySelectorAll('.one');
for (const [ index, el ] of elems.entries()) {
...
Object.values(names.reduce((acc, n) => {
const g = /^\d/.test(n[0]) ? n[0] : /^[А-Я]/i.test(n[0]) ? 'А-Я' : null;
g && (acc[g] = acc[g] || [ g ]).push(n);
return acc;
}, {}))
<div class="items"></div>
.<select>
<option value="*">Все</option>
<option value=".red">Красные</option>
<option value=".blue">Синие</option>
<option value=".green">Зеленые</option>
</select>
$('select').change(function() {
const selector = this.value;
const $items = $('.items >');
$items.filter(selector).slideDown();
$items.not(selector).slideUp();
});
const compareArrays = (a, b) => a.length === b.length && a.every((n, i) => n === b[i]);
+str.replace(/[^\d.]/g, '')
parseFloat(str.match(/[\d.]/g).join(''))
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)));