I am attempting to update this array of objects based on the object key. Each key maps to a registry which holds a method to format the data and return it and then update the object key. This recursive function works but cant handle an object key which has a nested object.
[
{
"myData": "test",
"nestedArr": [
{
"hello": "test",
"check": "test",
"nestedObject": {
"hello": "hello",
"nestedArr": [
{
"test": {
"value": "updateMe"
}
}
]
}
}
]
}
]
Here is my current implementation
const parse = async (arr) => {
return await Promise.all(
arr.map(async (obj) => {
await Promise.all(
Object.keys(obj).map(async (value) => {
if (Array.isArray(obj[value])) {
await parse(obj[value]);
}
try {
const method = registry[value];
const data = await method(obj, value);
obj[value] = data;
} catch (error) {}
})
);
return obj;
})
);
};
UPDATED:
const parse = async (arr) => {
return await Promise.all(
arr.map(async (obj) => {
await Promise.all(
Object.keys(obj).map(async (value) => {
if (Array.isArray(obj[value])) {
await parse(obj[value]);
}
try {
if (typeof obj[value] === "object") {
await parse([obj[value]]);
}
} catch (error) {}
try {
const method = registry[value];
const data = await method(obj, value);
obj[value] = data;
} catch (error) {}
})
);
return obj;
})
);
};
@Barmar pointed me in the right direction, updated the result. Works great not too concerned on the performance aspect as this result will be cached in redis