I have this object:
{
"id": "e8546367-d643-4da0-80dc-0980c9935ded",
"offsetSeconds": 86400,
"tasks": {
"id": "contactChampionInitialTalk",
"icon": "0",
"skills": [
"skillCommunityManager"
],
"name": {
"en": "Contact Champion"
},
"description": {
"en": "Initial talk, agreement on the theme, and potentiel availabilities for the event"
}
}
}
And I want to recursively go through it using JavaScript/TypeScript to get:
{
"id": "e8546367-d643-4da0-80dc-0980c9935ded",
"offsetSeconds": 86400,
"tasks": {
"id": "contactChampionInitialTalk",
"icon": "0",
"skills": [
"skillCommunityManager"
],
"name": "Contact Champion",
"description":"Initial talk, agreement on the theme, and potentiel availabilities for the event"
}
}
How can I do that? I have an array of multiple objects like this one
This function works but only for level 1 entries:
const replaceNestedValue = (obj, targetKey) => {
const newObj = { ...obj };
Object.entries(newObj).forEach(([key, value]) => {
if (value && typeof value === 'object' && !Array.isArray(value) && value[targetKey]) {
newObj[key] = value[targetKey];
}
});
return newObj;
};
2
Answers
You are checking for the
targetKey
too early. As you need to process the input recursively you should apply your function for any nested object. AndtargetKey
should only control actual replacement. For the innermost part something like this:Personally, I’d rather rewrite the function to not operate in-place but return the adjusted result.
You would need to first check what the data type is of the first argument (primitive, array or (other) object), and for each of these three categories determine what to do:
Code:
This solution considers the input immutable, i.e. it does not modify the original input, but returns new objects/arrays where mutation is needed.