мне ж понятно будет
const getKeys = obj =>
obj instanceof Object
? Object.entries(obj).flatMap(n => [ n[0], ...getKeys(n[1]) ])
: [];
const keys = getKeys(obj);
function* getKeys(obj) {
if (Object(obj) === obj) {
for (const k in obj) if (obj.hasOwnProperty(k)) {
yield k;
yield* getKeys(obj[k]);
}
}
}
const keys = Array.from(getKeys(obj));
// или
for (const n of getKeys(obj)) {
console.log(n);
}
const propsCount = 3;
.const newObj = Object.fromEntries(Object
.entries(obj)
.sort((a, b) => a[1] - b[1])
.slice(-propsCount)
);
Object
.entries(obj)
.sort((a, b) => b[1] - a[1])
.slice(propsCount)
.forEach(n => delete obj[n[0]]);
watch: {
данные: {
immediate: true,
handler(value) {
if (value какой нужен) {
делаете чего там вам надо
}
},
},
},
computed: {
данные() {
return [ this.данные1, this.данные2, /* ... */ ];
},
},
watch: {
данные: {
immediate: true,
handler(value) {
if (value.every(n => n какой нужен)) {
делаете чего там вам надо
}
},
},
},
mixin item(item)
.inform-items__item(class=`inform-items__item_bg_${item.bg}`)
app.directive(directives);
str.match(/(?<=>)\d+/g) ?? []
// или
str.match(/>\d+/g)?.map(n => n.slice(1)) ?? []
// или
Array.from(str.matchAll(/>(\d+)/g), n => n[1])
''.concat(...Array.from(str, n => n.repeat(2)))
// или
str.replace(/./g, '$&$&')
// или
str.replace(/./g, m => Array(3).join(m))
// или
str.replace(/(?=.)/g, (m, i) => str[i])
// или
[...str].flatMap(n => Array(2).fill(n)).join('')
// или
[].map.call(str, n => `${n}${n}`).join``
// или
str.split('').reduce((acc, n) => acc + n + n, '')
const averageAge = arr.reduce((acc, n) => acc + n.age, 0) / arr.length;
function avg(data, key = n => n) {
const getVal = key instanceof Function ? key : n => n[key];
let sum = 0;
let count = 0;
for (const n of data) {
sum += getVal(n);
count += 1;
}
return sum / count;
}
const averageAge = avg(arr, 'age');
.avg(Array(10).keys()) // 4.5
avg('12345', Number) // 3
avg(document.images, n => n.width) // сами посмотрите, сколько тут получится
arr.reduce((acc, n, i) => (
(!i || n === 1) && acc.push([]),
acc[acc.length - 1].push(n),
acc
), [])
const index = arr.reduce((min, n, i, a) => a[min]?.length <= n.length ? min : i, -1);
if (index !== -1) {
arr[index] = arr[index][0].toUpperCase() + arr[index].slice(1);
}
const [ indexes ] = arr.reduce((min, n, i) => (
n.length < min[1] && (min = [ [], n.length ]),
n.length === min[1] && min[0].push(i),
min
), [ [], Infinity ]);
indexes.forEach(n => arr[n] = arr[n].replace(/./, m => m.toUpperCase()));
const date = new Date(str.replace(/\S+/, m => m.split('.').reverse().join('-')));
const date = new Date(str.replace(/(\d+)\.(\d+)\.(\d+)/, '$3-$2-$1'));
const [ day, month, year, hours, minutes, seconds ] = str.split(/\D/);
const date = new Date(year, month - 1, day, hours, minutes, seconds);
const date = dayjs(str, 'DD.MM.YYYY HH:mm:ss').toDate();
function sort([...arr]) {
const max = arr.reduce((max, n) => max?.population > n.population ? max : n, null);
return arr.sort((a, b) => a === max ? -1 : b === max ? 1 : a.city.localeCompare(b.city));
}
const className = 'model';
const elements = document.querySelectorAll(`.${className}`);
// или
const elements = document.getElementsByClassName(className);
const getText = el => el.textContent;
// или
const getText = el => el.innerText;
// или (т.к. вложенных элементов нет, это тоже сработает как надо)
const getText = el => el.innerHTML;
const result = Array.from(elements, getText).join(', ');
// или
const result = ''.concat(...[...elements].flatMap((n, i) => (
n = getText(n),
i ? [ ', ', n ] : n
)));
// или
const result = Array.prototype.reduce.call(
elements,
(acc, n, i) => acc + (i ? ', ' : '') + getText(n),
''
);
const { x: countX = 0, o: countO = 0 } = Array
.from(str.toLowerCase())
.reduce((acc, n) => (acc[n] = (acc[n] ?? 0) + 1, acc), {});
const [ countX, countO ] = [ /x/i, /o/i ].map(n => ~-str.split(n).length);
почему-то в данные изменение попадает только после nextTick'а (т.е. когда что-то побуждает родительский компонент перерисоваться)