Skip to content

Instantly share code, notes, and snippets.

// Fetched from server
const nestedNumbers = [
[[0], [[[[[[[1, 2]]]]]]], [3]],
[[[4], [[5]]], [[[6, 7, 8]]]],
[9]
];
const incrementNestedNumbers = (arrayWithNums) => {
for (let i = 0; i < arrayWithNums.length; i++) {
if (Array.isArray(arrayWithNums[i])) { // if array
const nestedNumbers = [
[[0], [[[[[[[1, 2]]]]]]], [3]],
[[[4], [[5]]], [[[6, 7, 8]]]],
[9]
];
out = [];
for (let i = 0; i < 2; i++) {
for (let j = 0; j < 2; j++) {
out.push([ i, j ]);
}
}
console.log(out); // -> [[ 0, 0 ], [ 0, 1 ], [ 1, 0 ], [ 1, 1 ]]
sum();
sum(1, 1)();
sum(1)(5)(12)();
sum(1)(132, 4)();
sum(1, 2, 3)(7, 8, 9)(5)();
sum(1, 1)(4)(6, 13, 7)(2)(3)(2)(2, 1)();
const comments = [
{
text: 'comment 1',
comments: [
{
text: 'comment 2',
comments: [],
},
],
},
const traverseHtmlElement = (rootElement, _level = 0) => {
// Get all element's children stringified if any
let rootChildren = '';
if (rootElement.childElementCount) {
rootChildren = traverseHtmlElement(rootElement.firstElementChild, _level + 1);
}
// Get all element's siblings stringified if any
let rootSiblings = '';
const nextSibling = rootElement.nextElementSibling;
const bodyChildren = [...document.body.children];
for (let i = 0; i < bodyChildren.length; i++) {
// So... how do we get children of each body child?
analyseElement(bodyChildren[i]);
}
const flatToBase = array => array.reduce(
(accumulator, value) => accumulator.concat(
Array.isArray(value) ? flatToBase(value) : value
),
[],
);
flatToBase([[[[[[[ 42 ]]]], 36]]]); // -> [ 42, 36 ]
nestedArrays.flat(Infinity);
const mirrorNumbersUpTo = (num) => {
console.log(num);
if (num > 0) {
mirrorNumbersUpTo(num - 1);
console.log(num);
}
};
mirrorNumbersUpTo(5); // ➡️ logs 5, 4, 3, 2, 1, 0, 1, 2, 3, 4, 5