Last active
September 22, 2020 19:32
-
-
Save tsmx/cccd47b6bb9bfe53fc6f232f4e81b274 to your computer and use it in GitHub Desktop.
Traversing a JSON object in JavaScript for printing it out or applying functions/filters to every key/value - tracks the full path of every element, supports arrays, nested objects, arrays-in-arrays and objects-in-arrays
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// the sample simply logs every item with its corresponding path | |
// replace 'logKeyValue' with custom functions/filters/... that should be applied | |
function traverse(obj, path = []) { | |
Object.entries(obj).forEach(([key, val]) => { | |
if (val !== null && typeof val == 'object') { | |
if (Array.isArray(val)) { | |
for (let i = 0; i < val.length; i++) { | |
let elem = val[i]; | |
let itemKey = createKeyString(i, true); | |
let currentPath = Array.from(path); | |
currentPath.push(key); | |
if (elem !== null && typeof elem == 'object') { | |
currentPath.push(itemKey); | |
traverse(elem, currentPath); | |
} | |
else { | |
logKeyValue(itemKey, elem, currentPath, true); | |
} | |
} | |
} | |
else { | |
logKeyValue(createKeyString(key, false), val, path); | |
let currentPath = Array.from(path); | |
currentPath.push(createKeyString(key, false)); | |
traverse(val, currentPath); | |
} | |
} | |
else { | |
logKeyValue(createKeyString(key, Array.isArray(obj)), val, path); | |
} | |
}); | |
} | |
function createKeyString(key, isArrayKey = false) { | |
let keyVal = key; | |
if (isArrayKey) { | |
keyVal = '[' + keyVal + ']'; | |
} | |
return keyVal; | |
} | |
function logKeyValue(key, val, path) { | |
console.log((path.length > 0 ? (path.join('.') + '.') : '') + key + ' = ' + val); | |
} | |
const testobj = { | |
'arr': [0, 0], | |
'arr-in-arr': [0, 1, ['two', 'three', [4, 5, 6]]], | |
'number': 123, | |
'string': 'ttt', | |
'parent': { | |
'test': 1, | |
'child': { | |
'childval': 777 | |
}, | |
'array': [1, 2, 66, 9, { 'array-ob-key': 'zzz', 'array-in-array': ['a', 'b', 'c'] }, 900] | |
}, | |
'trail': 'testtesttest' | |
} | |
traverse(testobj); | |
// output: | |
// | |
// arr.[0] = 0 | |
// arr.[1] = 0 | |
// arr-in-arr.[0] = 0 | |
// arr-in-arr.[1] = 1 | |
// arr-in-arr.[2].[0] = two | |
// arr-in-arr.[2].[1] = three | |
// arr-in-arr.[2].2.[0] = 4 | |
// arr-in-arr.[2].2.[1] = 5 | |
// arr-in-arr.[2].2.[2] = 6 | |
// number = 123 | |
// string = ttt | |
// parent = [object Object] | |
// parent.test = 1 | |
// parent.child = [object Object] | |
// parent.child.childval = 777 | |
// parent.array.[0] = 1 | |
// parent.array.[1] = 2 | |
// parent.array.[2] = 66 | |
// parent.array.[3] = 9 | |
// parent.array.[4].array-ob-key = zzz | |
// parent.array.[4].array-in-array.[0] = a | |
// parent.array.[4].array-in-array.[1] = b | |
// parent.array.[4].array-in-array.[2] = c | |
// parent.array.[5] = 900 | |
// trail = testtesttest |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment