const select = document.querySelector('[name="id_car_mark"]');blackList:const options = Object.fromEntries(Array.from(select, n => [ n.innerText, n ]));
select.prepend(...blackList.reduce((acc, n) => ((n = options[n]) && acc.push(n), acc), []));select.append(...Array.prototype.filter.call(select, function(n) {
return !this.has(n.textContent);
}, new Set(blackList)));
$('form').on('input', function() {
const disabled = $('[type="text"]', this).get().some(n => n.value.length < 5);
$('[type="submit"]', this).prop({ disabled });
});document.querySelector('form').addEventListener('input', e => {
e.currentTarget.querySelector('[type="submit"]').disabled = Array
.from(e.currentTarget.querySelectorAll('[type="text"]'))
.some(n => n.value.length < 5);
});
.main-header__menu. Увели курсор на другой элемент - меню пропало..main-header__menu:hover + .main-header__wrap-submenu,
.main-header__wrap-submenu:hover {
opacity: 1;
visibility: visible;
}
<h1 class="header">click me</h1>
<h1 class="header">click me</h1>
<h1 class="header">click me</h1>const text = [ 'hello, world!!', 'fuck the world', 'fuck everything' ];
const selector = '.header';$(selector).click(function() {
const index = -~$(this).data('index') % text.length;
$(this).text(text[index]).data('index', index);
}).data('index', -1);
// или
document.querySelectorAll(selector).forEach(function(n) {
n.addEventListener('click', this);
}, ({ currentTarget: t }) => {
const index = (t.dataset.index ?? 0) % text.length;
t.textContent = text[index];
t.dataset.index = index + 1;
});
<Route path="/:id" component={SinglePhoto} /> <Route path="/history" component={SearchHistory} />
computed: {
tasksGroupedByCategory() {
return this.tasks.reduce(
(acc, n) => (acc[n.category].push(n), acc),
Object.fromEntries(this.boards.map(n => [ n.name, [] ]))
);
},
},<div v-for="b in boards">
<h2>{{ b.title }}</h2>
<div v-for="n in tasksGroupedByCategory[b.name]">
{{ n.title }}
</div>
</div>
const findPath = (obj, val) =>
obj === val
? []
: Object.entries(obj instanceof Object ? obj : {}).reduce((found, n) => (
found || ((found = findPath(n[1], val)) && found.unshift(n[0])),
found
), null);function findPath(obj, val) {
for (const stack = [ [ obj, [] ] ]; stack.length;) {
const [ n, keys ] = stack.pop();
if (Object.is(n, val)) {
return keys;
} else if (n instanceof Object) {
stack.push(...Object.entries(n).map(([ k, v ]) => [ v, [ ...keys, k ] ]));
}
}
return null;
}
setInterval($items => {
$items.each(i => $items.eq(i).css({
left: `${Math.random() * window.innerWidth}px`,
top: `${Math.random() * window.innerHeight}px`,
}));
}, 500, $элементыКоторымНадоЗадаватьСлучайныеКоординаты);
const formTemplate = `<здесь html-код формы>`;
$(document).on('change', 'селектор чекбокса', function() {
const $el = $(this).closest('селектор формы');
if (this.checked) {
$el.after(formTemplate);
} else {
$el.nextAll('селектор формы').remove();
}
});
@click="testClickGo().restart"targets: '.testVal',data: () => ({
anime: null,
}),
mounted() {
this.anime = this.$anime({
targets: this.$refs.animeEl,
translateX: 200,
delay: 800,
});
},<h1 ref="animeEl">hello, world!!</h1>
<button @click="anime.restart()">click me</button>
v-model.
const group = (arr, key, childrenKey) =>
Object.values(arr.reduce((acc, { [childrenKey]: children, ...n }) => {
const item = acc[n[key]] = acc[n[key]] ?? n;
if (children) {
children = (item[childrenKey] ?? []).concat(children);
item[childrenKey] = group(children, key, childrenKey);
}
return acc;
}, {}));const grouped = group(products, 'name', 'childItems');
switch (true) {
case one.includes(inp):
...
break;
case two.includes(inp):
...
break;
}const arr = [
{
values: [ ... ],
action: () => { ... },
},
{
values: [ ... ],
action: () => { ... },
},
];arr.find(n => n.values.includes(inp))?.action();
preg_match_all('~(?<=<p>).+?(?=</p>)~', $str, $match);
$data = array_map(function($n) {
return explode('-', $n, 2);
}, $match[0]);
str.split('"').filter((n, i) => i & 1)
// или
Array.from(str.matchAll(/"([^"]*)"/g), n => n[1])
// или
(function get(reg) {
const m = reg.exec(str);
return m ? [ m[0].slice(1, -1), ...get(reg) ] : [];
})(/".*?"/g)
// или
Array.prototype.reduce.call(str, (acc, n) => {
if (n !== '"') {
acc[1]?.push(n);
} else if (acc[1]) {
acc[0].push(acc[1].join(''));
acc[1] = null;
} else {
acc[1] = [];
}
return acc;
}, [ [], null ])[0]