Skip to content

Instantly share code, notes, and snippets.

@abenteuerzeit
Created September 30, 2023 10:38
Show Gist options
  • Save abenteuerzeit/c113f45efc0c837f0df176c471515a91 to your computer and use it in GitHub Desktop.
Save abenteuerzeit/c113f45efc0c837f0df176c471515a91 to your computer and use it in GitHub Desktop.
Print nested array contents in the style of a file tree
let arr = [
// arr[0]
[
// arr[0][0]
[
// arr[0][0][0]
"unshift",
// arr[0][0][1]
[
// arr[0][0][1][0]
[
// arr[0][0][1][0][0]
false
],
// arr[0][0][1][1]
[
// arr[0][0][1][1][0]
1,
// arr[0][0][1][1][1]
2,
// arr[0][0][1][1][2]
3
]
],
// arr[0][0][2]
[
// arr[0][0][2][0]
"complex",
// arr[0][0][2][1]
"nested"
],
],
// arr[0][1]
[
// arr[0][1][0]
"loop",
// arr[0][1][1]
"shift",
// arr[0][1][2]
[
// arr[0][1][2][0]
6,
// arr[0][1][2][1]
7,
// arr[0][1][2][2]
1000,
],
// arr[0][1][3]
"method",
],
],
// arr[1]
[
// arr[1][0]
"concat",
// arr[1][1]
[
// arr[1][1][0]
false,
// arr[1][1][1]
true,
],
// arr[1][2]
"spread",
// arr[1][3]
"array",
],
// arr[2]
[
// arr[2][0]
"mutate",
// arr[2][1]
1327.98,
// arr[2][2]
"splice",
// arr[2][3]
"slice",
// arr[2][4]
"push",
],
// arr[3]
[
// arr[3][0]
"iterate",
// arr[3][1]
[
// arr[3][1][0]
1.3849,
// arr[3][1][1]
7,
],
// arr[3][2]
"8.4876",
// arr[3][3]
"arbitrary",
// arr[3][4]
"depth",
],
];
function printNestedStructure(struct, path = [], prefix = '') {
if (Array.isArray(struct)) {
for (let i = 0; i < struct.length; i++) {
const currentPath = [...path, i];
const linePrefix = (i === struct.length - 1) ? '└──' : '├──';
const continuationPrefix = (i === struct.length - 1) ? ' ' : '│ ';
if (typeof struct[i] === 'object') {
console.log(prefix + linePrefix + `${currentPath.map(idx => `[${idx}]`).join('')}`);
printNestedStructure(struct[i], currentPath, prefix + continuationPrefix);
} else {
console.log(prefix + linePrefix + `${currentPath.map(idx => `[${idx}]`).join('')} → ${struct[i]}`);
}
}
} else if (typeof struct === 'object') {
const keys = Object.keys(struct);
for (let i = 0; i < keys.length; i++) {
const currentPath = [...path, keys[i]];
const linePrefix = (i === keys.length - 1) ? '└──' : '├──';
const continuationPrefix = (i === keys.length - 1) ? ' ' : '│ ';
if (typeof struct[keys[i]] === 'object') {
console.log(prefix + linePrefix + `${keys[i]}`);
printNestedStructure(struct[keys[i]], currentPath, prefix + continuationPrefix);
} else {
console.log(prefix + linePrefix + `${keys[i]} → ${struct[keys[i]]}`);
}
}
}
}
console.log(".");
printNestedStructure(arr);
/* OUTPUT
.
├──[0]
│ ├──[0][0]
│ │ ├──[0][0][0] → unshift
│ │ ├──[0][0][1]
│ │ │ ├──[0][0][1][0]
│ │ │ │ └──[0][0][1][0][0] → false
│ │ │ └──[0][0][1][1]
│ │ │ ├──[0][0][1][1][0] → 1
│ │ │ ├──[0][0][1][1][1] → 2
│ │ │ └──[0][0][1][1][2] → 3
│ │ └──[0][0][2]
│ │ ├──[0][0][2][0] → complex
│ │ └──[0][0][2][1] → nested
│ └──[0][1]
│ ├──[0][1][0] → loop
│ ├──[0][1][1] → shift
│ ├──[0][1][2]
│ │ ├──[0][1][2][0] → 6
│ │ ├──[0][1][2][1] → 7
│ │ └──[0][1][2][2] → 1000
│ └──[0][1][3] → method
├──[1]
│ ├──[1][0] → concat
│ ├──[1][1]
│ │ ├──[1][1][0] → false
│ │ └──[1][1][1] → true
│ ├──[1][2] → spread
│ └──[1][3] → array
├──[2]
│ ├──[2][0] → mutate
│ ├──[2][1] → 1327.98
│ ├──[2][2] → splice
│ ├──[2][3] → slice
│ └──[2][4] → push
└──[3]
├──[3][0] → iterate
├──[3][1]
│ ├──[3][1][0] → 1.3849
│ └──[3][1][1] → 7
├──[3][2] → 8.4876
├──[3][3] → arbitrary
└──[3][4] → depth
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment