const table = document.querySelector('тут селектор вашей таблицы');table.querySelectorAll('tbody td:last-child').forEach(
(n, i, { [~-i]: { innerText: prev } = {} }) =>
n.innerText !== prev && (n.innerHTML = `<a href="#">${n.innerText}</a>`)
);
// или
let prev = null;
for (const { rows } of table.tBodies) {
for (const { lastElementChild: n } of rows) {
const curr = n.textContent;
if (curr !== prev) {
const a = document.createElement('a');
a.href = '#';
a.append(...n.childNodes);
n.append(a);
prev = curr;
}
}
}
return { ...user, isFollowed: !this.isFollowed };
const chunks = (arr, chunkSize) => Array.from(
{ length: Math.ceil(items.length / chunkSize) },
(n, i) => items.slice(i * chunkSize, (i + 1) * chunkSize)
);const Card = props => (
<div className="style-review__slide-row">
<a className="style-card style-review__style-card" href="#">
<div className="style-card__overlay">
<div className="style-card__title">{props.title}</div>
<div className="style-card__category">
{props.category}
</div>
</div>
</a>
</div>
);const chunkedItems = chunks(items, 3);<Swiper>
{chunkedItems.map(cardItems => (
<SwiperSlide className="swiper-slide style-review__slide">
{cardItems.map(n => <Card {...n} />)}
</SwiperSlide>
))}
</Swiper>
// создаём новый массив
const newArr = arr.map(({ name, ...n }) => (n.user_name = name, n));
// изменяем существующий:
arr.forEach(n => (n.user_name = n.name, delete n.name));// keys - объект вида { старый_ключ_1: 'новый_ключ_1', старый_ключ_2: 'новый_ключ_2', ... }
const renameKeys = (obj, keys) =>
Object.fromEntries(Object
.entries(obj)
.map(([ k, v ]) => [ keys.hasOwnProperty(k) ? keys[k] : k, v ])
);
const newArr = arr.map(n => renameKeys(n, { name: 'user_name' }));function renameKeys(obj, keys) {
for (const n of Object.entries(keys)) {
if (obj.hasOwnProperty(n[0])) {
obj[n[1]] = obj[n[0]];
delete obj[n[0]];
}
}
}
arr.forEach(n => renameKeys(n, { name: 'user_name' }));
state = {
markers: [],
maxMarkersNum: 666,
}
onClick = ({ latlng }) => {
this.setState(({ markers, maxMarkersNum }) => ({
markers: markers.length >= maxMarkersNum
? []
: [ ...markers, latlng ],
}));
}<Map onClick={this.onClick}>
{this.state.markers.map(n => <Marker position={n} />)}
</Map>
В App.vue я запрашиваю все продукты из БД
store.dispatch('GET_PRODUCTS').then(() => {
new Vue({ ... });
});watch: {
'$store.state.products': {
immediate: true,
handler(products) {
if (products) {
// можно что-то сделать
}
},
},
},
$arr = array_slice(explode(' ', $str), 1);.array_map(fn($n) => trim($n, '"'), $arr).
const isSumEven = arr => arr.reduce((p, c) => p ^ c, 1) & 1;
// или
const isSumEven = arr => !(arr.filter(n => n % 2).length % 2);
// или
const isSumEven = arr => Number.isInteger(arr.reduce((acc, n) => acc + n, 0) / 2);
// или
const isSumEven = arr => /[02468]$/.test(eval(arr.join('+')) ?? 0);const result = arr.filter(isSumEven);.
function split(arr, numParts) {
const partSize = arr.length / numParts | 0;
return Array
.from({ length: numParts }, (n, i) => i * partSize)
.map((n, i, a) => arr.slice(n, a[i + 1]));
}function split(arr, numParts) {
const partSize = arr.length / numParts | 0;
const numLooseItems = arr.length % numParts;
return Array.from(
{ length: numParts },
function(_, i) {
return arr.slice(this(i), this(i + 1));
},
i => i * partSize + Math.min(i, numLooseItems)
);
}
coords.reduce((acc, n, i, a) => (
acc.push(n),
(n.line !== a[i + 1]?.line) && acc.push({ ...n, value: 0 }),
acc
), [])coords.flatMap((n, i, a) =>
i === ~-a.length || n.line !== a[-~i].line
? [ n, { ...n, value: 0 } ]
: n
)
data() {
return {
isChecked: false,
}
},
methods: {
toggleSwitch() {
this.isChecked = !this.isChecked
this.$emit('input', this.isChecked)
},
},props: {
value: Boolean,
},- :class="{ 'ivu-switch-checked': isChecked }"
+ :class="{ 'ivu-switch-checked': value }"- @click.prevent="toggleSwitch"
+ @click.prevent="$emit('input', !value)"
elem.classList.add('check'); elem.querySelector('input[type="checkbox"]').remove();
data: () => ({
tasks: [],
taskId: 0,
newTaskText: '',
}),
methods: {
addTask() {
this.tasks.push({
id: ++this.taskId,
text: this.newTaskText,
done: false,
edit: false,
});
this.newTaskText = '';
},
},<ul>
<li v-for="(n, i) in tasks" :key="n.id" :class="{ check: n.done }" @dblclick="n.edit = !n.done">
<input type="checkbox" v-if="!n.done" v-model="n.done">
<input type="text" v-if="!n.done && n.edit" v-model="n.text" @keyup.enter="n.edit = false">
<template v-else>{{ n.text }}</template>
<input type="button" class="del" value="+" @click="tasks.splice(i, 1)">
</li>
</ul>
<input type="text" v-model="newTaskText" @keyup.enter="addTask">
const data = Array.prototype.reduce.call(
document.querySelector('form').elements,
(acc, { name, value }) => (
name.match(/\w+/g).reduce((p, c, i, a) =>
p[c] ??= (-~i < a.length ? {} : value)
, acc),
acc
),
{}
);Для
<input type="text" name="iuowye[rrr][]" value="1"> <input type="text" name="iuowye[rrr][]" value="2"> <input type="text" name="iuowye[rrr][]" value="3">
работать не будет.
const data = Array
.from(document.querySelectorAll('form [name]'))
.reduce((acc, n) => {
const keys = n.name.match(/\w+|\[\w*\]/g).map(n => n.replace(/^\[|\]$/g, ''));
const key = keys.pop();
const isArr = !key;
const values = keys.reduce((p, c, i, a) => {
return p[c] ??= (i === a.length - 1 && isArr ? [] : {});
}, acc);
values[isArr ? values.length : key] = n.value;
return acc;
}, {});