Skip to content

Instantly share code, notes, and snippets.

@JosePedroDias

JosePedroDias/modules.js

Last active Jan 5, 2020
Embed
What would you like to do?
traverse to manipulate JSON
// yarn list --json > modules.json
const fs = require('fs');
const O = require('./modules.json');
function visit(o) {
if (typeof o === 'object' && o !== null) {
if (o instanceof Array) {
// change array (noop)
for (const it of o) {
visit(it);
}
} else {
// change object
delete o.shadow;
delete o.color;
delete o.hint;
delete o.depth;
for (const k of Object.keys(o)) {
visit(o[k]);
}
}
}
}
visit(O);
fs.writeFileSync('modules2.json', JSON.stringify(O.data.trees, null, 2));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.