const average = arr => arr.reduce((acc, n) => acc + n, 0) / arr.length;
const extractValues = (group, item, key) =>
item[key].forEach(({ id, name, value }) => {
((group.values[id] ||= { id, name })[key] ||= []).push(value);
});
const result = Object
.values(arr
.flatMap(n => n.properties.groups)
.reduce((acc, n) => (
acc[n.id] ||= {
id: n.id,
name: n.name,
wellBeing: [],
values: {},
},
acc[n.id].wellBeing.push(n['well-being']),
extractValues(acc[n.id], n, 'needs'),
extractValues(acc[n.id], n, 'provision'),
acc
), {}))
.map(n => ({
id: n.id,
name: n.name,
value: average(n.wellBeing),
values: Object.values(n.values).map(m => (
m.needs = average(m.needs),
m.provision = average(m.provision),
m
))
}));
const xxx = ([...str]) => [...new Set(Array.from(
{ length: str.length * 10 },
(n, i) => str.map((m, j) => (j === (i / 10 | 0)) ? i % 10 : m).join('')
))];function xxx(str) {
const arr = str.split('');
const result = [ str ];
for (const [ i, n ] of arr.entries()) {
for (let j = 0; j < 10; j++) {
if (j !== +n) {
arr[i] = j;
result.push(arr.join(''));
}
}
arr[i] = n;
}
return result;
}
data: () => ({
items: [ 1, 25, 32, 4, 5 ],
filter: false,
}),
computed: {
filteredItems() {
return this.items.filter(n => n < 10);
},
},<label>
<input type="checkbox" v-model="filter">
Фильтровать
</label>
<ul>
<li v-for="n in (filter ? filteredItems : items)">{{ n }}</li>
</ul>
Я не меняю prop href
@Prop(String) href = '';
It's not supported to define eachdefaultproperty like@Prop() prop = 'default value'.
const first = document.querySelector('#first');
const second = document.querySelector('#second');const hideOptions = {
1: [ '12', '14', '15' ],
2: [ '1', '11', '16' ],
};first.addEventListener('change', function() {
Array.prototype.forEach.call(second, function(n) {
n.hidden = this.includes(n.value);
}, hideOptions[this.value] ?? []);
});.hidden {
display: none;
}first.onchange = e => {
const toHide = hideOptions[e.target.value];
for (const n of second) {
n.classList.toggle('hidden', toHide?.indexOf(n.value) > -1);
}
};
const nav = document.querySelector('.nav');
const itemSelector = '.nav-link';
const image = document.querySelector('.preview-img');
const category = document.querySelector('.preview-head');
function updatePreview(data) {
image.src = data.img;
category.textContent = data.category;
}nav.querySelectorAll(itemSelector).forEach(function(n) {
n.addEventListener('mouseenter', this);
}, e => updatePreview(e.target.dataset));nav.addEventListener('mouseover', e => {
const data = e.target.closest(itemSelector)?.dataset;
if (data) {
updatePreview(data);
}
});
metricsColumn: true.computed: {
metricsColumns() {
return this.headers.filter(n => n.metricsColumn);
},
},<template
v-for="(n, i) in metricsColumns"
#[`item.${n.value}`]="{ item: { metrics: { [i]: item } } }"
>
<v-chip
:color="colors[item.metricsValueMarkName]"
:key="n.value"
dark
>
{{ item.metricsValue }}
</v-chip>
</template>
function output(value) {
const digits = document.querySelectorAll('селектор span`ов');
const { length } = digits;
Array
.from(`${value}`.padStart(length, 0).slice(-length))
.forEach((n, i) => digits[i].innerText = n);
}function output(value) {
document.querySelector('селектор родительского элемента span`ов').innerHTML = Array
.from(`${value}`, n => `<span>${n}</span>`)
.join('');
}
const [ first, ...rest ] = document.querySelectorAll('[type="checkbox"]');
const onChange = () => first.checked = rest.every(n => n.checked);
first.addEventListener('change', () => rest.forEach(n => n.checked = first.checked));
rest.forEach(n => n.addEventListener('change', onChange));
img:const { images } = new DOMParser().parseFromString(html, 'text/html');
// или
const images = document
.createRange()
.createContextualFragment(html)
.querySelectorAll('img');
// или
const images =
(el => (el.innerHTML = html, el.getElementsByTagName('img')))
(document.createElement('div'));img извлечь значения src и srcset:const getSrc = img => [ img.src, img.srcset ];
// или
const getSrc = img => [ 'src', 'srcset' ].map(n => img.getAttribute(n));
// или
const getSrc = ({ attributes: a }) => [ a.src.value, a.srcset.value ];const result = Array.from(images, getSrc);
// или
const result = Array.prototype.map.call(images, getSrc);
// или
const result = [];
for (const n of images) {
result.push(getSrc(n));
}
// или
const result = [];
for (let i = 0; i < images.length; i++) {
result[i] = getSrc(images[i]);
}
// или
const result = (function get(i, n = images.item(i)) {
return n ? [ getSrc(n), ...get(i + 1) ] : [];
})(0);
const parent = document.querySelector('селектор элемента, из которого надо кого-то удалить');
const elements = parent.children;
const toRemove = [ 3, 1, 666, 2 ];toRemove
.map(n => elements[n])
.filter(Boolean)
.forEach(n => n.remove());
// или
for (const n of toRemove.sort((a, b) => b - a)) {
elements.item(n)?.replaceWith();
}
// или
parent.replaceChildren(...Array.prototype.filter.call(
elements,
((index, _, i) => !index.has(i)).bind(null, new Set(toRemove))
));
// или
Array.prototype.reduceRight.call(
elements,
(_, n, i) => toRemove.includes(i) && parent.removeChild(n),
null
);
// или
parent.innerHTML = Array
.from(elements, n => n.outerHTML)
.filter((_, i) => !~toRemove.indexOf(i))
.join('');
// или
parent
.querySelectorAll(toRemove.map(n => `:scope > :nth-child(${-~n})`))
.forEach(n => n.outerHTML = '');
:style="mapStyles"data() { return { mapStyles: { styles: [
mapStyles: { на options: {:style="mapStyles" на :options="options"
data: () => ({
contact: '',
}),
methods: {
onClick() {
if (this.contact) {
this.$emit('close');
}
},
},<input v-model="contact"><button @click="onClick">
const getStrs = (arrs, len) => [
'*'.repeat(len + 2),
...arrs.flatMap(arr => arr
.reduce((acc, n) => {
let g = acc[acc.length - 1];
(g && (g[1] + n.length + !!g[0].length) <= len) || acc.push(g = [ [], 0 ]);
g[1] += n.length + !!g[0].length;
g[0].push(n);
return acc;
}, [])
.map((n, i, a) => `*${n[0].join(' ')[a.length > 1 ? 'padStart' : 'padEnd'](len, ' ')}*`)),
'*'.repeat(len + 2),
];
console.log(getStrs(textArray, 16).join('\n'));
.children(i)$.each(res, function(i, n) {
this
.eq(i)
.html(`${n[1]}<input type="hidden" value="${n[0]}">`)
.css('display', 'block');
}.bind($('.box-results-search').children()));
{ key: первый индекс, где встречается key }:const keyIndex = arr.reduce((acc, n, i) => (acc[n.key] ??= i, acc), {});id. Можно отсортировать существующий массив:arr.sort((a, b) => (keyIndex[a.key] - keyIndex[b.key]) || (a.id - b.id));const sorted = (arr, keys) => arr
.map(n => [ n ].concat(keys(n)))
.sort((a, b) => {
let diff = 0;
for (let i = 0; ++i < a.length && !(diff = a[i] - b[i]);) ;
return diff;
})
.map(n => n[0]);
const sortedArr = sorted(arr, n => [ keyIndex[n.key], n.id ]);