const insert = (str, substr, ...indices) => [ 0 ]
.concat(indices)
.filter(n => 0 <= n && n <= str.length)
.sort((a, b) => a - b)
.map((n, i, a) => str.slice(n, a[i + 1]))
.join(substr);
// или
const insert = (str, substr, ...indices) => indices
.sort((a, b) => b - a)
.reduce((acc, n) => (
0 <= n && n <= str.length && acc.splice(n, 0, substr),
acc
), [...str])
.join('');
// или
const insert = (str, substr, ...indices) =>
''.concat(...Array.from(
{ length: -~str.length },
function(_, i) {
return substr.repeat(this[i] ?? 0) + str.charAt(i);
},
indices.reduce((acc, n) => (acc[n] = -~acc[n], acc), {})
));
const insert = (str, substr, ...indices) => indices
.filter(n => 0 <= n && n <= str.length)
.sort((a, b) => a - b)
.reduceRight((acc, n, i) =>
acc.replace(RegExp(`(?<=^.{${n}})`), substr)
, str);
// ваш случай
const str = insert(`${number}`, '-', 3, 5, 8, 10);
// вставлять можно больше одного символа
insert('abc', ' --> ', 1, 2) // 'a --> b --> c'
// можно по одному индексу несколько раз делать вставку
insert('abc', '~', 0, 0, 0) // '~~~abc'
// за границы строки вставка не выполняется,
// за исключением позиции сразу же после конца строки
insert('abc', '!', -1, 3, 4) // 'abc!'
вторая не работает
.grade-item
обрабатывать, а только те, у кого в предках тот же .grade-item-block
, что и у кликнутого.const container = document.querySelector('.nav-student-new-lesson');
const blockSelector = '.grade-item-block';
const itemSelector = `${blockSelector} .grade-item`;
const colors = {
grades: [
[ 5, 'rgba(150, 255, 0, 0.3)' ],
[ 3, 'rgba(255, 150, 0, 0.3)' ],
[ 1, 'rgba(255, 0, 0, 0.3)' ],
],
default: 'white',
};
function updateGrade(item) {
const items = item.closest(blockSelector).querySelectorAll(itemSelector);
const grade = 1 + Array.prototype.indexOf.call(items, item);
const color = colors.grades.find(n => n[0] <= grade)[1];
items.forEach((n, i) => n.style.background = i < grade ? color : colors.default);
}
container.addEventListener('click', ({ target: t }) =>
(t = t.closest(itemSelector)) && updateGrade(t)
);
.grade-item
:container.querySelectorAll(itemSelector).forEach(function(n) {
n.addEventListener('click', this);
}, e => updateGrade(e.currentTarget));
function generator($str, $params) {
$result = [];
if (count($params)) {
$key = key($params);
$values = is_array($params[$key]) ? $params[$key] : [ $params[$key] ];
unset($params[$key]);
foreach ($values as $val) {
array_push($result, ...generator(str_replace("{{$key}}", $val, $str), $params));
}
} else {
$result[] = $str;
}
return $result;
}
const container = document.querySelector('#list');
const activeClass = 'active';
container.firstElementChild.classList.add(activeClass);
document.addEventListener('keydown', e => {
const nextElKey = ({
ArrowDown: 0,
ArrowUp: 1,
})[e.key];
const currEl = container.querySelector(`:scope > .${activeClass}`);
const nextEl =
currEl[[ 'nextElementSibling', 'previousElementSibling' ][nextElKey]]/* ??
container[[ 'firstElementChild', 'lastElementChild' ][nextElKey]]*/;
// если надо, чтобы при переходе от последнего к следующему элементу
// активным становился первый, а при переходе от первого к предыдущему
// активным становился последний, достаточно удалить многострочное
// комментирование выше
if (nextEl) {
currEl.classList.remove(activeClass);
nextEl.classList.add(activeClass);
}
});
const next = function(step) {
this[1][this[0]].classList.remove(activeClass);
this[0] = Math.max(0, Math.min(this[1].length - 1, this[0] + step));
// или, если надо, чтобы выделение по кругу бегало
// this[0] = (this[0] + step + this[1].length) % this[1].length;
this[1][this[0]].classList.add(activeClass);
}.bind([ 0, container.children ]);
next(0);
document.addEventListener('keydown', ({ key }) => {
const step = +(key === 'ArrowDown') || -(key === 'ArrowUp');
if (step) {
next(step);
}
});
думаю что тут проблема из за proxy
Vue.toRaw(this.map).geoObjects.add(placemark);
this.map = Vue.markRaw(new ymaps.Map(this.$refs.map, {
...
card_gallery.on('click', function(swiper, e) {
const index = swiper.slides.indexOf(e.target.closest('.swiper-slide'));
if (index !== -1) {
single_gallery.slideTo(index);
}
});
slideToClickedSlide: true
в настройки card_gallery. Object.values(Object.fromEntries(arr.map(n => [ n.user.id, n ])))
// или, если в результирующий массив должны попадать те из "одинаковых" элементов,
// что расположены в исходном массиве первыми
Object.values(arr.reduce((acc, n) => (acc[n.user.id] ??= n, acc), {}))
// или, если также надо сохранять взаимное расположение элементов
arr.filter(function({ user: { id: n } }) {
return !(this[n] = this.hasOwnProperty(n));
}, {})
function* unique(data, key = n => n) {
const getKey = key instanceof Function ? key : n => n[key];
const keys = new Set;
for (const n of data) {
const k = getKey(n);
if (!keys.has(k)) {
keys.add(k);
yield n;
}
}
}
const result = [...unique(arr, n => n.user.id)];
.Array.from(unique([{id: 1}, {id: 2}, {id: 1}, {id: 1} ], 'id')) // [{id: 1}, {id: 2}]
''.concat(...unique('ABBACACCCBA')) // 'ABC'
.for (const n of unique(Array(20).keys(), n => Math.sqrt(n) | 0)) {
console.log(n); // 0 1 4 9 16
}
.list-enter-from {
transition-group
следует добавить параметр appear
. fetch('https://jsonplaceholder.typicode.com/todos/1')
1
.const [data, setData] = useState(null);
{data.map(item => (
null
метод map
? - погуглите, разберитесь. Ну и замените null
на []
. const radios = document.querySelectorAll('[name="radios"]');
const selects = Array.from(radios, n => n.nextElementSibling);
const onChange = e => selects.forEach(n => n.disabled = n !== e.target.nextElementSibling);
radios.forEach(n => n.addEventListener('change', onChange));
data.each_value{|n|
puts("#{n[:name]}: вес #{n[:weight]} г., количество #{n[:qty]} шт.")
}
const arithmeticProgression = ({ length, a1 = 0, d = 1 }) =>
Array.from(
{ length },
(n, i) => a1 + i * d
);
const arr = arithmeticProgression({
length: 10,
a1: 6,
d: 3,
});
function* arithmeticProgression(a1, d, length) {
for (let i = 0; i < length; i++) {
yield a1 + i * d;
}
}
for (const n of arithmeticProgression(100, 10, 5)) {
console.log(n);
}
console.log(Array.from(arithmeticProgression(10, -7, 10)));
def fucking_filter(arr, max_count, key=lambda n: n):
filtered = []
count = {}
for n in arr:
k = key(n)
count[k] = count.get(k, 0) + 1
if count[k] <= max_count:
filtered.append(n)
return filtered
for n in fucking_filter(genres, N):
print(n)
хочу сделать задержку ввода
methods: {
onInput: debounce(function(val) {
this.debouncedInput = val;
}, 500),
...
<v-text-field
@input="onInput"
...
computed: {
count() {
return this.orders.map(n => n.count);
},
},
watch: {
count: debounce(function(newVal, oldVal) {
this.debouncedInput = newVal.find((n, i) => n !== oldVal[i]);
}, 500),
},
data: () => ({
links: [ 'tasks', 'kanban', 'activity', 'calendar', 'files' ],
}),
ul
li(v-for="n in links")
router-link(:to="{ name: n }") {{ n }}
.marker
div(v-for="n in links" :class="[ `select${n}`, $route.name === n ? 'active' : '' ]")
span
const certificate = ref({});
shortingTechItems(certificate.pto)
прилетает undefined
В чем проблема
как исправить?
- const shortingTechItems = (item) => {
- const arr = [];
- item.map((i) => {
- arr.push(Number(i.replace(/\D+/g, "")));
- });
- return arr;
- };
+ const shortingTechItems = items => items.map(n => +n.replace(/\D/g, ''));
props: [ 'value' ],
provide() {
return {
radioGroup: this,
};
},
props: [ 'value' ],
inject: [ 'radioGroup' ],
<input
type="radio"
:value="value"
:checked="radioGroup.value === value"
@change="radioGroup.$emit('input', value)"
>
props: [ 'modelValue' ],
emits: [ 'update:modelValue' ],
setup(props, { emit }) {
provide('radioGroupValue', computed({
get: () => props.modelValue,
set: v => emit('update:modelValue', v),
}));
},
props: [ 'value' ],
setup() {
return {
radioGroupValue: inject('radioGroupValue'),
};
},
<input
type="radio"
:value="value"
v-model="radioGroupValue"
>
const voidTags = [ 'input', 'img', 'br', 'hr', ещё какой-то тэг, и ещё, ... ];
function createHTML(data) {
const attrs = Object
.entries(data.attrs ?? {})
.map(n => `${n[0]}="${n[1]}"`)
.join(' ');
const startTag = `<${data.tagName}${attrs && (' ' + attrs)}>`;
if (voidTags.includes(data.tagName)) {
return startTag;
}
const children = (data.subTags ?? [])
.map(createHTML)
.join('');
return `${startTag}${data.text ?? ''}${children}</${data.tagName}>`;
}