if (total_sum >= rand) {
function getRandom(arr, key) {
const rand = Math.random() * arr.reduce((acc, n) => acc + n[key], 0);
let sum = 0;
return arr.find(n => (sum += n[key]) > rand);
}
const obj = getRandom(data, 'weight');
document.addEventListener('click', ({ target: t }) => {
if (t.matches('.oldValue')) {
let input = t;
while (!(input = input.previousElementSibling).matches('.value')) ;
input.value = t.textContent;
}
});
const inputs = document.querySelectorAll('.value');
const spans = [...document.querySelectorAll('.oldValue')];
const onClick = ({ target: t }) => inputs[spans.indexOf(t)].value = t.innerText;
spans.forEach(n => n.addEventListener('click', onClick));
document.addEventListener('click', ({ target: t }) => {
if (t.classList.contains('oldValue')) {
t.closest('селектор общей обёртки').querySelector('.value').value = t.innerHTML;
}
});
swiper-container-initialized
, можно назначать нужные стили в зависимости от его наличия:.swiper-container:not(.swiper-container-initialized) {
.swiper-button-prev,
.swiper-button-next,
.swiper-pagination {
display: none;
}
}
value: Boolean,
, а в <toggle-button
-:value="value"
@input="$emit('input', $event)"
<toggle-button
добавить v-on="$listeners"
. let isBtn1Clicked = false;
button1.addEventListener('click', () => isBtn1Clicked = true);
button2.addEventListener('click', () => {
if (isBtn1Clicked) {
// ...
}
});
button1.addEventListener('click', e => e.target.classList.add('clicked'));
button2.addEventListener('click', () => {
if (button1.classList.contains('clicked')) {
// ...
}
});
button2.disabled = true;
button1.addEventListener('click', () => button2.disabled = false);
button2.addEventListener('click', () => {
// ...
});
button2.hidden = true;
button1.addEventListener('click', () => button2.hidden = false);
button2.addEventListener('click', () => {
// ...
});
button1.addEventListener('click', () => {
button2.addEventListener('click', () => {
// ...
});
}, { once: true });
const obj = Object.fromEntries(arr.map(n => [ n.name, n.number ]));
// или
const obj = arr.reduce((acc, n) => (acc[n.name] = n.number, acc), {});
// или
const obj = Object.assign({}, ...arr.map(n => ({ [n.name]: n.number })));
function toObj(data, key, val = n => n) {
const getKey = key instanceof Function ? key : n => n[key];
const getVal = val instanceof Function ? val : n => n[val];
const obj = {};
for (const n of data) {
obj[getKey(n)] = getVal(n);
}
return obj;
}
const obj = toObj(arr, 'name', 'number');
// {Kolya: '5', Olga: '10'}
const charCodes = toObj('abc', n => n.charCodeAt());
// {97: 'a', 98: 'b', 99: 'c'}
<input name="xxx" value="69">
<input name="yyy" value="187">
<input name="zzz" value="666">
const inputValues = toObj(document.querySelectorAll('input'), 'name', 'value');
// {xxx: '69', yyy: '187', zzz: '666'}
идентичны
l
и 1
совсем не одно и то же.console.log(this.$slots.header) // не работает
created () {
function App() {
const [ seconds, setSeconds ] = React.useState(60);
const [ timerActive, setTimerActive ] = React.useState(false);
React.useEffect(() => {
if (seconds > 0 && timerActive) {
setTimeout(setSeconds, 100, seconds - 1);
} else {
setTimerActive(false);
}
}, [ seconds, timerActive ]);
return (
<div>
{seconds
? <React.Fragment>
<button onClick={() => setTimerActive(!timerActive)}>
{timerActive ? 'stop' : 'start'}
</button>
<div>{seconds}</div>
</React.Fragment>
: <button onClick={() => setSeconds(60)}>ещё раз</button>
}
</div>
);
}
const prices = {
'Москва': 100,
'Санкт-Петербург': 200,
'Казань': 300,
};
$('form').on('change', function() {
const [ city1, city2 ] = $('select', this).get().map(n => n.value);
const price = city1 === city2
? prices[city1]
: prices[city1] + prices[city2];
$('input', this).val(price);
}).change();