const getPrimitives = data =>
data instanceof Object
? Object.values(data).flatMap(getPrimitives)
: [ data ];
const result = getPrimitives(obj);function* getPrimitives(data) {
if (data instanceof Object) {
for (const k in data) if (data.hasOwnProperty(k)) {
yield* getPrimitives(data[k]);
}
} else {
yield data;
}
}
const result = [...getPrimitives(obj)];function getNestedData(data, test) {
const result = [];
for (const stack = [ data ]; stack.length;) {
const n = stack.pop();
if (n instanceof Object) {
stack.push(...Object.values(n).reverse());
}
if (test(n)) {
result.push(n);
}
}
return result;
}
const result = getNestedData(obj, x => x !== Object(x));
if(links.every(link => isVisited(link))) {
unblockSpecialLink();
}(async () => {
const result = await {
then(resolve) {
console.log('Привет сахар');
setTimeout(() => resolve(10), 3000);
}
};
console.log('Ух ты, данные из таймера:', result);
})();(async () => {
try {
await {
then(_, reject) {
console.log('Привет сахар');
reject(new Error('Ошибочка'));
}
};
console.log('Это никогда не выполнится');
} catch (e) {
console.log('Ух ты, эксепшн:', e);
}
})();console.log('Промисы никуда не делись?', (async () => {})() instanceof Promise)
-1 ? 1 : 2 значение -1 приводится к типу boolean и получается true-1 == true значение true приводится к числу и получается 1=== и !==) и приводить типы явно
const result = arr.reduce((acc, n, i) => (
(i & 1) || acc.push(0),
acc[~-acc.length] += n,
acc
), []);function* chunked(data, chunkSize) {
const iter = data[Symbol.iterator]();
for (let chunk = [], n; !n?.done && (n = iter.next());) {
if (!n.done) {
chunk.push(n.value);
}
if (chunk.length === chunkSize || (n.done && chunk.length)) {
yield chunk;
chunk = [];
}
}
}
function sum(data, val = n => n) {
const getVal = val instanceof Function ? val : n => n[val];
let result = 0;
for (const n of data) {
result += getVal(n);
}
return result;
}const result = Array.from(chunked(arr, 2), n => sum(n));
function formatDate(str) {
const d = new Date(str.replace('_', ' 1, '));
d.setMonth(d.getMonth() - 1);
return d.toLocaleDateString('en-US', {
month: 'short',
year: 'numeric',
}).replace(' ', '_');
}
formatDate('Feb_2021') // "Jan_2021"
formatDate('Jan_2000') // "Dec_1999"
this.setState(({ items }) => ({
items: items.map((n, i) => ({ ...n, id: i })),
}));state = {
items: this.props.items.map((n, i) => ({ ...n, id: i })),
}
[1, 2, 3, 4, 5].sort((a, b) => b % 2 - a % 2 || (a % 2 ? b - a : a - b))