Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
transforms values from json file (simple version)
const O = {
a: 2,
b: '3',
c: [5, [6, 'a'], {e:'c', f:2}],
d: {z:[3, 'd']}
};
function deepTransformValues(mapFn, o) {
if (o instanceof Array) {
const l = o.length;
const o2 = new Array(l);
for (let i = 0; i < l; ++i) {
o2[i] = deepTransformValues(mapFn, o[i]);
}
return o2;
} else if (typeof o === 'object') {
const o2 = {};
Object.keys(o).forEach(k => {
o2[k] = deepTransformValues(mapFn, o[k]);
});
return o2;
} else {
return mapFn(o);
}
}
function tr(v) { return v + '_'; };
const O2 = deepTransformValues(tr, O);
//console.log(JSON.stringify(O2, null, 2));
console.log(O2);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment