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' ];
$('.header').click(function() {
const index = $(this).data('index') || 0;
$(this).text(text[index]).data('index', (index + 1) % text.length);
});
<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) =>
Object.is(obj, val)
? []
: Object.entries(obj instanceof Object ? obj : {}).reduce((found, n) => {
if (!found) {
found = findPath(n[1], val);
if (found) {
found.unshift(n[0]);
}
}
return found;
}, null);
function findPath(obj, val) {
for (const stack = [ [ obj, [] ] ]; stack.length;) {
const [ n, keys ] = stack.pop();
if (Object.is(n, val)) {
return keys;
}
if (n instanceof Object) {
stack.push(...Object.entries(n).reverse().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, keyProp, childrenProp) =>
Object.values(arr.reduce((acc, { [childrenProp]: children, ...n }) => {
const item = acc[n[keyProp]] = acc[n[keyProp]] || n;
if (children) {
children = (item.childItems || []).concat(children);
item[childrenProp] = group(children, keyProp, childrenProp);
}
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)
// или
str.match(/".*?"/g)?.map(n => n.slice(1, -1)) ?? []
// или
Array.from(str.matchAll(/"([^"]*)"/g), n => n[1])
// или
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]