Estoy intentando actualizar esta matriz de objetos en función de la clave del objeto. Cada clave se asigna a un registro que contiene un método para formatear los datos y devolverlos y luego actualizar la clave del objeto. Esta función recursiva funciona pero no puede manejar una clave de objeto que tiene un objeto anidado.
[ { "myData": "test", "nestedArr": [ { "hello": "test", "check": "test", "nestedObject": { "hello": "hello", "nestedArr": [ { "test": { "value": "updateMe" } } ] } } ] } ]Aquí está mi implementación actual
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; }) ); };ACTUALIZADO:
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 me señaló en la dirección correcta, actualizó el resultado. Funciona muy bien, no se preocupa demasiado por el aspecto del rendimiento, ya que este resultado se almacenará en caché en redis