computed: {
model() {
return new Proxy(Object.fromEntries(this.obj2.map(n => [ n.model, null ])), {
get: (target, prop) => typeof prop === 'string'
? prop.split('.').reduce((p, c) => p?.[c], this.obj)
: target[prop],
set: (target, prop, value) => {
const keys = prop.split('.');
const key = keys.pop();
const obj = keys.reduce((p, c) => (!p.hasOwnProperty(c) && this.$set(p, c, {}), p[c]), this.obj);
this.$set(obj, key, value);
return true;
},
});
},
},<div v-for="(v, k) in model">
<input v-model="model[k]">
</div>computed: {
model() {
return this.obj2.map(n => {
const keys = n.model.split('.');
const key = keys.pop();
const obj = keys.reduce((p, c) => p?.[c], this.obj);
return obj && { obj, key };
}).filter(Boolean);
},
},<div v-for="n in model">
<input v-model="n.obj[n.key]">
</div>
li:not(:first-child, :last-child)li:not(:first-child):not(:last-child)li {
/* тут стили для всех элементов */
}
li:first-child,
li:last-child {
/* здесь сбрасываете стили, установленные выше */
}
const replaceValues = (val, test, replacer, key) =>
test(key, val)
? replacer(val)
: val instanceof Object
? Object.entries(val).reduce((acc, [ k, v ]) => (
acc[k] = replaceValues(v, test, replacer, k),
acc
), val.constructor())
: val;function replaceValues(val, test, replacer) {
const stack = [];
const clones = new Map;
const getClone = (v, k) =>
test(k, v)
? replacer(v)
: v instanceof Object
? (clones.has(v) || stack.push([ v, clones.set(v, v.constructor()).get(v) ]),
clones.get(v))
: v;
if (val instanceof Object) {
for (getClone(val); stack.length;) {
const [ source, target ] = stack.pop();
for (const k in source) if (Object.hasOwn(source, k)) {
target[k] = getClone(source[k], k);
}
}
}
return getClone(val);
}const newData = replaceValues(
data,
k => k?.endsWith('Date'),
v => v.replace(/(\d+)-(\d+)-/, '$2.$1.')
);
получить массив
Object.values.
присвоить первому массиву значения второго только без первого элемента
$('.content_toggle').click(function() {
const $button = $(this);
$(`#box-${this.id.split('-').pop()}`).slideToggle(300, function() {
const isHidden = $(this).is(':hidden');
$button
.text(isHidden ? 'Показать текст' : 'Скрыть текст')
.toggleClass('open', !isHidden);
});
return false;
});$(this).closest('здесь селектор общего предка').find('.content_block')$(this).next()$('.content_block').eq($('.content_toggle').index(this))
echo preg_replace_callback('~\[(.+?)\]~', fn($m) => $array[$m[1]] ?? $m[0], $message);
По клику на кнопку вызывается мутация, в которою передаётся id.
Я так понимаю, потому что метод возвращает только false/true, а не измененный item.
id передается, проверял через консоль
...mapMutations(['chekedItem', 'deleteItem']), itemSucces(id) { this.chekedItem({id}); },
chekedItem: (state, item) => item.checked = !item.checked,
$(document).on('click', 'a[id^="answer_"]', e => $(`#comment_${e.target.id}`).toggle());
succesItem='succesItem'
Vue.options.filters. Соответственно:const filteredValue = Vue.options.filters.someFilter(value);
// или, если вспомнить, что компоненты являются экземплярами Vue
const filteredValue = this.constructor.options.filters.someFilter(value);
function setNested(root, ...args) {
const val = args.pop();
const key = (args = args
.flat(Infinity)
.flatMap(n => typeof n === 'string' ? n.split('.') : n))
.pop();
args.reduce((p, c) => p[c] ??= {}, root)[key] = val;
}setNested(data, '0.arr.0.str', 'hello, world!!');
setNested(data, 0, 'arr', 1, 'num', 666);Интересуют готовые решения
Нужно изменить этот массив, преобразовав все строки с датами в нём, независимо от того, как глубоко они вложены, в объекты.
const replaceNested = (val, replacer) =>
val instanceof Object
? Object.entries(val).reduce((acc, [ k, v ]) => (
acc[k] = replaceNested(v, replacer),
acc
), val.constructor())
: replacer(val);const newData = replaceNested(
data,
x => typeof x === 'string' && /^\d{2}\.\d{2}\.\d{4}$/.test(x)
? new Date(x.split('.').reverse().join('-'))
: x
);
const valueKey = 'number';
const percentKey = 'percent';const sum = arr.reduce((acc, n) => acc + n[valueKey], 0);
const getPercent = n => n[valueKey] / sum * 100;const newArr = arr.map(n => ({ ...n, [percentKey]: getPercent(n) }));
// или
arr.forEach(n => n[percentKey] = getPercent(n));