input
, чтобы v-model
обновил данные компонента:methods: {
removeLastChar({ target: t }) {
t.value = t.value.slice(0, -1);
t.dispatchEvent(new Event('input'));
},
...
},
<input
@focus="removeLastChar"
...
>
function getLeaves(tree) {
const result = [];
for (const stack = [...tree]; stack.length;) {
const n = stack.pop();
if (Array.isArray(n.children) && n.children.length) {
stack.push(...n.children);
} else {
result.push(n);
}
}
return result.reverse();
}
function getLeaves(tree) {
const result = [];
const stack = [];
for (let arr = tree, i = 0; i < arr.length || stack.length; i++) {
if (i === arr.length) {
[ i, arr ] = stack.pop();
} else if (arr[i].children instanceof Array && arr[i].children.length) {
stack.push([ i, arr ]);
[ i, arr ] = [ -1, arr[i].children ];
} else {
result.push(arr[i]);
}
}
return result;
}
function getLeaves([...tree]) {
for (let i = 0; i < tree.length; i++) {
const c = tree[i].children;
if (c?.constructor === Array && c.length) {
tree.splice(i--, 1, ...c);
}
}
return tree;
}
const groupSum = (arr, idKey, ...sumKeys) =>
Object.values(arr.reduce((acc, n) => (
acc[n[idKey]] ??= sumKeys.reduce((group, k) => (group[k] = 0, group), {...n}),
sumKeys.forEach(k => acc[n[idKey]][k] += n[k]),
acc
), {}));
const result = groupSum(arr, 'product', 'price', 'selling_price', 'quantity');
[
[ '.btn-all', 'click', onGetDataClick ],
[ '.elem button', 'click', onPlusMinusClick ],
[ '.amount', 'input', onAmountInput ],
].forEach(([ selector, type, handler ]) => {
document.querySelectorAll(selector).forEach(n => n.addEventListener(type, handler));
});
function onGetDataClick(e) {
const data = Array.prototype.flatMap.call(
e.target.closest('.child-block').querySelectorAll('.elem'),
n => Array(+n.querySelector('.amount').value).fill(+n.dataset.id)
);
console.log(data);
}
function onPlusMinusClick({ target }) {
const input = target.closest('.elem').querySelector('.amount');
input.value -= target.innerText === '+' ? -1 : 1;
input.dispatchEvent(new Event('input'));
}
function onAmountInput({ target: t }) {
t.value = Math.max(t.min, Math.min(t.max, t.value | 0));
}
sortOpen() {
this.$emit('sortOpen', this.sortWindowOpen = !this.sortWindowOpen);
},
<TableControl @sortOpen="addStatic = $event"></TableControl>
const editTodo = (id, title) => {
setTodos(todos.map(n => n.id === id
? { ...n, title }
: n
));
};
function TodoItem({ complete, title, id, removeTodo, toggleTodo, editTodo }) {
const [ isEdit, setIsEdit ] = useState(false);
const [ editValue, setEditValue ] = useState('');
return (
<div className="todo-item">
<input type="checkbox" checked={complete} onChange={() => toggleTodo(id)} />
{isEdit
? <>
<input value={editValue} onChange={e => setEditValue(e.target.value)} />
<button onClick={() => (setIsEdit(false), editTodo(id, editValue))}>Save</button>
<button onClick={() => setIsEdit(false)}>Cancel</button>
</>
: <span onDoubleClick={() => (setIsEdit(true), setEditValue(title))}>{title}</span>
}
<button onClick={() => removeTodo(id)}>X</button>
</div>
);
}
const obj = {
a: {
b: {
c: {
xxx: 666,
},
},
},
};
const proxy = new Proxy(obj, {
get(target, key) {
return key.split('.').reduce((p, c) => p?.[c], target);
},
set(target, key, val) {
const keys = key.split('.');
const k = keys.pop();
keys.reduce((p, c) => p[c] ??= {}, target)[k] = val;
return true;
},
});
console.log(proxy['a.b.c.xxx']); // 666
proxy['x.y.z'] = 187;
console.log(obj.x.y.z); // 187
// имена свойств можно передавать в виде массива
const pick = (obj, keys) => Object.fromEntries(keys.map(n => [ n, obj[n] ]));
const newObj = pick(obj, [ 'b', 'c' ]);
// или как отдельные значения
const pick = (obj, ...keys) => keys.reduce((acc, n) => (acc[n] = obj[n], acc), {});
const newObj = pick(obj, 'b', 'c');
const pickExisting = (obj, keys) =>
Object.fromEntries(keys.filter(n => n in obj).map(n => [ n, obj[n] ]));
const pickOwn = (obj, keys) =>
keys.reduce((acc, n) => (Object.hasOwn(obj, n) && (acc[n] = obj[n]), acc), {});
const pick = (obj, filterFn) => Object.fromEntries(Object.entries(obj).filter(filterFn));
const obj1 = pick(obj, ([ k, v ]) => 'abc'.includes(k) && v % 2); // {a: 1, c: 3}
const obj2 = pick(obj, n => n[1] > 2); // {c: 3, d: 5}
$toStr = fn($arr) => implode('', array_map(fn($k) => "[$k:]$arr[$k][:$k]", array_keys($arr)));
print_r(array_map($toStr, $lang));
data: () => ({
meetupId: null,
meetupData: null,
}),
watch: {
meetupId(val) {
fetch(`https://course-vue.javascript.ru/api/meetups/${val}`)
.then(r => r.json())
.then(r => this.meetupData = r);
},
},
<label v-for="i in 5">
<input type="radio" name="meetupId" :value="i" v-model="meetupId">
{{ i }}
</label>
<pre>{{ meetupData }}</pre>
nextSlide() {
---> nextSlide = () => {
.onclick = this.nextSlide;
---> .onclick = () => this.nextSlide();
.onclick = this.nextSlide;
---> .onclick = this.nextSlide.bind(this);
this
. str.split(/(?<=\S)\s*(?=[А-ЯЁ])/).join(' ')
// или
str.replace(/(?<=\S)(?=[А-ЯЁ])/g, ' ')
// или
str.replace(/(\S)(?=[А-ЯЁ])/g, '$1 ')
// или
str.replace(/[А-ЯЁ]/g, (m, i) => i && str[~-i] !== ' ' ? ' ' + m : m)
почему ошибку выдает?
It should take an array of numbers as its argument and return the two highest numbers within the array.
function twoOldestAges($ages) {
sort($ages);
return array_slice($ages, -2);
}
function twoOldestAges($ages) {
$a = -INF;
$b = -INF;
foreach ($ages as $n) {
if ($n > $a) {
$b = $a;
$a = $n;
} else if ($n > $b) {
$b = $n;
}
}
return [ $b, $a ];
}
function search(nums, target, searchMin) {
for (let iMin = 0, iMax = nums.length - 1; iMin <= iMax; ) {
const index = (iMin + iMax) / 2 | 0;
const num = nums[index];
if (num === target && num !== nums[index + (searchMin ? -1 : 1)]) {
return index;
} else if (num <= target - (searchMin ? 1 : 0)) {
iMin = index + 1;
} else {
iMax = index - 1;
}
}
return -1;
}
function searchMinMax(nums, target) {
const iMin = search(nums, target, true);
return iMin === -1
? []
: [ iMin, search(nums, target, false) ];
}
@update:modelValue="$emit('change', $event.target.value)"
update:modelValue
?@change="$emit('update:modelValue', $event.target.value)"
model: { prop: 'modelValue', event: 'update:modelValue' },
BREAKING:v-bind
's.sync
modifier and componentmodel
option are removed and replaced with an argument on v-model;