Make an object with the same structure but modified values

I'm trying to do some cool stuff with .mdx as strings being parsed in getStaticProps and got to an ok point but everything went to shit when I tried to put those strings into an object since I need to serialize them and want to keep the object structure for that sweet autocomplete in the frontend. Long context out of the way, let's say I have:
const obj = {
something: "...",
somethingNested: {
iLove: "...",
typeSafety: "..."
}
}
const obj = {
something: "...",
somethingNested: {
iLove: "...",
typeSafety: "..."
}
}
How should I go about making another object, with the same structure and keys but where every string got passed to a serialize function I have? I tried something like
const recurse = (item: Object | string) => {
if(typeof item !== 'string'){
const keys = Object.keys(item);
keys.forEach(key => recurse(item[key]));
// I don't know how I should loop through the items in a way that would allow me to return the key and value correctly here 😭
} else {
// I don't know how to return the result to the right key here 😭
return serialize(item)
}
}
const recurse = (item: Object | string) => {
if(typeof item !== 'string'){
const keys = Object.keys(item);
keys.forEach(key => recurse(item[key]));
// I don't know how I should loop through the items in a way that would allow me to return the key and value correctly here 😭
} else {
// I don't know how to return the result to the right key here 😭
return serialize(item)
}
}
but kept going in circles with no success Hope everything here made sense because I really need help with this
3 Replies
bedesqui
bedesqui•2y ago
Stack Overflow
map function for objects (instead of arrays)
I have an object: myObject = { 'a': 1, 'b': 2, 'c': 3 } I am looking for a native method, similar to Array.prototype.map that would be used as follows: newObject = myObject.map(function (value, ...
Unknown User
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View
bedesqui
bedesqui•2y ago
taking this step back and looking at it as a clone helped so much, ty!
Want results from more Discord servers?
Add your server