document.addEventListener('click', e => {
const btn = e.target.closest('.more-btn');
for (const n of document.querySelectorAll('.more-btn')) {
n.classList.toggle('active', n === btn && !n.classList.contains('active'));
}
// или
document.querySelectorAll('.more-btn').forEach(function(n) {
n.classList[n === this ? 'toggle' : 'remove']('active');
}, e.target.closest('.more-btn'));
});
Если нам нужно прокинуть данные от родителя к ребенку на сколь угодно глубокий уровень, мы можем воспользоваться provide/inject
, а если нам надо в обратную сторону?
function weightedRandom(arr, key = () => 1) {
const val = key instanceof Function ? key : n => n[key];
const max = arr.reduce((acc, n) => acc + val(n), 0);
return () => {
let rand = Math.random() * max;
return arr.find(n => (rand -= val(n)) < 0);
};
}
const getRandomPhotosArray = weightedRandom([
[ 9, truePhotos ],
[ 1, funnyPhotos ],
], 0);
function getPhoto() {
const photos = getRandomPhotosArray()[1];
return photos[Math.random() * photos.length | 0];
}
new ApexCharts(..., {
...
tooltip: {
...
y: {
title: {
formatter: (seriesName, seriesData) => ...,
},
formatter: (value, seriesData) => ...,
},
},
});
<div class="tab" @click="$router.push('/')">
router-link
. Там добавление класса уже реализовано, осталось только стили ему прописать. $values = array_combine(
array_column($example2, 'SECTION_ID'),
array_column($example2, 'VALUE')
);
$example3 = array_map(function($n) use($values) {
$n['VALUE'] = $values[$n['SECTION_ID']];
return $n;
}, $example1);
{
title: data.title,
deadLineTimestamp: new Date(data.deadLine).getTime(),
description: data.description,
}
function createTreeFromArray(arr, key, parentKey) {
const tree = Object.fromEntries(arr.map(n => [ n[key], { ...n } ]));
return Object.fromEntries(Object.entries(tree).filter(([ , n ]) => {
return !(tree[n[parentKey]] && ((tree[n[parentKey]].children ??= {})[n[key]] = n));
}));
}
const tree = createTreeFromArray(arr, 'uid', 'parentUID');
И со слотами, и без слотов, и как только не пытался уже.
menu.insertAdjacentHTML('beforeend', arr
.map(n => `<li>${n}</li>`)
.join('')
);
// или
menu.append(...arr.reduce((acc, n) => (
(acc[acc.length] = document.createElement('li')).textContent = n,
acc
), []));
// или
for (const n of arr) {
menu.appendChild(document.createElement('li'));
menu.lastChild.innerText = n;
}
let li = document.createElement('li')
внутрь цикла. const teamNamesObj = Object.fromEntries(teamNames.map(n => [ n.id, n.name ]));
const eplWithNames = epl.map(n => ({ ...n, team_name: teamNamesObj[n.team_id] }));
epl.forEach(function(n) {
n.team_name = this[n.team_id];
}, teamNames.reduce((acc, n) => (acc[n.id] = n.name, acc), {}));