Would you like to clone this notebook?

When you clone a notebook you are able to make changes without affecting the original notebook.

Cancel

prefixing-strings-in-objects

node v14.20.1
version: 1.0.0
endpointsharetweet
const response = { data: { status: 200, response: { value: 777, model: { x: [1,2,3,'string',{prop:'hello'}], name: 'some string', description: 'another string', nested: { value: 'yet another string', } } } } } function transformRecursive(data, transformer) { if (data === null) { return data } else if (['string', 'number', 'boolean', 'undefined'].includes(typeof data)) { if (typeof data === 'string') { return transformer(data) } return data } else if (Array.isArray(data)) { return data.map(item => transformRecursive(item, transformer)) } else if (data instanceof Object) { return Object.keys(data).reduce((obj, key) => { return {...obj, [key]: transformRecursive(data[key], transformer)} }, {}) } return data } const tr = transformRecursive(response, x => `LOVELY_PREFIX_${x}`) console.log(tr)
Loading…

no comments

    sign in to comment