const obj = {
val: 1,
valueOf() {
return this.val ^= 1;
},
};
// или
const obj = {
val: '1',
toString() {
return this.val = '1'.slice(this.val.length);
},
};
// или
const obj = {
val: true,
[Symbol.toPrimitive]() {
return this.val = !this.val;
},
};
console.log(obj == false, obj == true); // true true
console.log(...Array.from({ length: 5 }, () => +obj)); // 0 1 0 1 0
function mustStay(n) {
for (const m of arr2) {
if (m.content.insuranceType === n.value) {
return false;
}
}
return true;
}
// или
const mustStay = n => arr2.every(m => m.content.insuranceType !== n.value);
// или
const mustStay = function(n) {
return !this.has(n.value);
}.bind(new Set(arr2.map(n => n.content.insuranceType)));
const filteredArr1 = arr1.filter(mustStay);
arr1.reduceRight((_, n, i, a) => mustStay(n) || a.splice(i, 1), null);
// или
arr1.splice(0, arr1.length, ...arr1.filter(mustStay));
// или
let numDeleted = 0;
for (const [ i, n ] of arr1.entries()) {
arr1[i - numDeleted] = n;
numDeleted += !mustStay(n);
}
arr1.length -= numDeleted;
const steps = [ /* ... */ ];
let currentStep = 0;
const quizQuestion = document.querySelector('.quiz__question');
const quizAnswers = document.querySelector('.quiz__answers');
const prevBtn = document.querySelector('.quiz__button_prev');
const nextBtn = document.querySelector('.quiz__button_next');
prevBtn.addEventListener('click', () => nextStep(-1));
nextBtn.addEventListener('click', () => nextStep(+1));
function nextStep(stepChange) {
currentStep = Math.max(0, Math.min(steps.length - 1, currentStep + stepChange));
prevBtn.disabled = currentStep === 0;
nextBtn.disabled = currentStep === steps.length - 1;
quizQuestion.innerHTML = steps[currentStep].question;
quizAnswers.innerHTML = steps[currentStep].answers.map(n => `<li>${n}</li>`).join('');
}
nextStep(0);
<div data-filter="service">
<div class="sign">Streaming service</div>
<div class="item">Netflix</div>
<div class="item">HBO Max</div>
<div class="item">Hulu</div>
</div>
<div data-filter="genre">
<div class="sign">Movie genre</div>
<div class="item">Comedy</div>
<div class="item">Action</div>
<div class="item">Horror</div>
<div class="item">Drama</div>
<div class="item">Fantasy</div>
</div>
<li class="card" data-service="Netflix" data-genre="Comedy">
const cardSelector = '.card';
const hiddenCardClass = 'hidden';
const filterSelector = '[data-filter]';
const filterItemSelector = `${filterSelector} .item`;
const activeFilterItemClass = 'item_active';
document.querySelector('.filters').addEventListener('click', e => {
const item = e.target.closest(filterItemSelector);
if (!item) {
return;
}
item.closest(filterSelector).querySelectorAll(filterItemSelector).forEach(n => {
n.classList[n === item ? 'toggle' : 'remove'](activeFilterItemClass);
});
const values = Array.from(
e.currentTarget.querySelectorAll(`.${activeFilterItemClass}`),
n => [ n.closest(filterSelector).dataset.filter, n.innerText ]
);
document.querySelectorAll(cardSelector).forEach(n => {
n.classList.toggle(hiddenCardClass, values.some(m => n.dataset[m[0]] !== m[1]));
});
});
return getValue(objectValus[prop], property);
const getValue = (obj, key) => Object
.values(obj ?? {})
.reduce((found, n) => found ?? getValue(n, key), obj?.[key]);
function solve(arr) {
const last = arr.reduce((acc, n, i) => (acc[n] = i, acc), {});
return arr.filter((n, i) => last[n] === i);
}
(function() {
function func(arg) {
console.log(arg, this);
}
let arg = 1;
const f1 = () => func(arg);
const f2 = func.bind(this, arg);
arg = 2;
f1();
f2();
}).call('hello, world!!');
Array.from(str, n => n < 5 ? 0 : 1).join('')
// или
[...str].reduce((acc, n) => acc + +(n >= 5), '')
// или
''.concat(...str.split('').map(n => Math.floor(n / 5)))
// или
[].map.call(str, n => -~-~-~n >> 3).join``
// или
str.replace(/./g, m => Number('56789'.includes(m)))
// или
str.replace(/[1-4]/g, 0).replace(/[5-9]/g, 1)
const newArr = Array.from(
{ length: arr.length / 2 },
(n, i) => ({ ...arr[i], ...arr[i + arr.length / 2] })
);
const numKeys = new Set(arr.flatMap(Object.keys)).size;
const newArr = arr.reduce((acc, n, i, a) => (
Object.assign(acc[i % (a.length / numKeys)] ??= {}, n),
acc
), []);
// или
const numKeys = Object.keys(Object.assign({}, ...arr)).length;
const numObjs = arr.length / numKeys;
const newArr = Array.from(
{ length: numObjs },
(n, i) => Object.assign({}, ...Array.from(
{ length: numKeys },
(m, j) => arr[j * numObjs + i]
))
);
const newArr = arr.reduce((acc, n) => {
const [ [ k, v ] ] = Object.entries(n);
const i = acc[0][k] = (acc[0][k] ?? -1) + 1;
(acc[1][i] ??= {})[k] = v;
return acc;
}, [ {}, [] ])[1];
<a>
. Соответственно, не будет и атрибута href
. А у null
не будет метода substr
. О чём вы должны были узнать сами, из сообщения об ошибке - если бы открыли консоль.target
следует использовать currentTarget. url.match(/.+\//)[0]
// или
url.replace(/[^\/]+$/, '')
// или
url.slice(0, url.lastIndexOf('/') + 1)
// или
url.split(/(?<=\/)/).slice(0, -1).join('')
// или
[...url].reduceRight((acc, n) => (acc || n === '/') ? n + acc : acc, '')
const where = '.list';
const what = '.content';
const $targets = $(where).children();
$(what).each((i, n) => $targets.eq(i).append(n));
const targets = document.querySelector(where).children;
document.querySelectorAll(what).forEach((n, i) => targets[i]?.append(n));
Object.values(data).flat().find(n => n.title === search)
Object.fromEntries(Object
.entries(data)
.map(n => [ n[0], n[1].filter(m => m.title.toLowerCase().includes(search.toLowerCase())) ])
.filter(n => n[1].length)
)
str.slice(1, -1).split(', ')
// или
JSON.parse(str.replace(/[а-яё]+/g, '"$&"'))
// или
str.match(/[^\s\[\],]+/g) ?? []