Created
July 11, 2017 20:07
-
-
Save alhafoudh/eafbfea9189385e243cd5461689b3b23 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
const _ = require('lodash'); | |
function printKeys(key, obj, parentKeyPath = '') { | |
const keyPath = `${parentKeyPath}.${key}`; | |
console.log(keyPath); | |
if (_.isArray(obj) && !_.isEmpty(obj)) { | |
printKeys(key, _.head(obj), keyPath); | |
} else if (_.isObject(obj)) { | |
const firstKey = _.head(_.keys(obj)); | |
if (_.toNumber(firstKey).toString() === firstKey.toString()) { | |
const firstObj = obj[firstKey]; | |
printKeys(firstKey, firstObj, keyPath); | |
} else { | |
_.map(obj, (newObj, newKey) => printKeys(newKey, newObj, keyPath)); | |
} | |
} | |
} | |
let content = ''; | |
process.stdin.resume(); | |
process.stdin.on('data', (buf) => { | |
content += buf.toString(); | |
}); | |
process.stdin.on('end', () => { | |
const json = JSON.parse(content); | |
printKeys('ROOT', json, '', 0); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment