function getURLs($arr, $path = []) {
$urls = [];
foreach ($arr as $item) {
array_push($path, $item['slug']);
array_push($urls, '/'.implode('/', $path).'/', ...getURLs($item['children'], $path));
array_pop($path);
}
return $urls;
}
const result = Object.values(arr.reduce((acc, n) => {
(acc[n.name] ??= ({ ...n, count: 0 })).count++;
return acc;
}, {}));
function uniqueWithCount(data, key, countKey) {
const getKey = key instanceof Function ? key : n => n[key];
const unique = new Map;
for (const n of data) {
const k = getKey(n);
unique.set(k, unique.get(k) ?? { ...n, [countKey]: 0 }).get(k)[countKey]++;
}
return unique;
}
const result = [...uniqueWithCount(arr, 'name', 'count').values()];
это вообще реально?
foreach ($arr as $item) {
$id = $item['id_product_attribute'];
if (!isset($result[$id])) {
$result[$id] = $item;
} else {
$result[$id]['attribute_name'] .= ', '.$item['attribute_name'];
}
}
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
)