Estoy tratando de analizar una estructura JSON. La estructura json se parece a esto:
{ children: [ { type: "p", children: [{ text: "" }] }, { type: "social_embed", children: [{ text: "" }] source_url: "some_url" }, { type: "p", children: [{ type: "p", children: [{ type: "p", children: [{ text: "" }] }] }] }, ] }La salida se verá así:
{ children: [ { type: "p", children: [{ text: "" }] }, { type: "p", children: [{ text: "some_url" }] }, { type: "p", children: [{ type: "p", children: [{ type: "p", children: [{ text: "" }] }] }] }, ] }Este es el código que estoy intentando:
if (currentBlock.type == "card" || currentBlock.type =="card_body") { parsedBlocks.map((block: any, index: any) => { block.children = parseBlocks(block.children) }) console.log("Blocks after parsing", parsedBlocks) editor.insertFragment(parsedBlocks); return true } const parseBlocks = (blocks: any): any => { blocks.forEach((block: any) => { console.log("Block ", block) if (block.type == "social_embed") { const newBlock = { type: "p", children: [ { text: block.source_url } ] } blocks[blocks.indexOf(block)] = newBlock } if (block.children) { return parseBlocks(block.children) } }) return blocks }Quiero recorrer recursivamente todos los niños hasta que no haya ninguna propiedad de niños en el objeto y cuando encuentre el objeto con el tipo: "social_embed", quiero reemplazarlo con el tipo: "p" y el texto como source_url y modificar toda la matriz, Los niños pueden tener un anidamiento ilimitado, pero social_embed no puede tener nada dentro de sus niños que no sea {texto: ""}
Qué tal algo como esto:
const parse = node => { if (node.type === "social_embed") { return { type: "p", children: [{ text: node.source_url}] } } return node.children ? { ...node, children: node.children.map(parse) } : node; }https://replit.com/@jamiedixon/ParseTree#index.js
Si desea ir un paso más allá, puede definir visitantes para los nodos según el type y procesarlos de esa manera.
const socialEmbed = node => ({ type: "p", children: [{ text: node.source_url }] }) const visitors = { "social_embed": [socialEmbed] } const parse = node => { const _visitors = visitors[node.type] || [x => x]; const result = _visitors.reduce((agg, fn) => fn(agg), node); return result.children ? { ...result, children: result.children.map(parse) } : result; }Puede asignar un nuevo objeto a los niños y tomar un nuevo objeto del anterior.
const update = ({ children = [], ...object }) => { if (object.type === "social_embed") { const type= 'p', text = object.source_url; return { type, children: [{ text }] }; } children = children.map(update); return children.length ? { ...object, children } : object; }, tree = { children: [{ type: "p", children: [{ text: "" }] }, { type: "social_embed", children: [{ text: "" }], source_url: "some_url" }, { type: "p", children: [{ type: "p", children: [{ type: "p", children: [{ text: "" }] }] }] }] }; tree.children = tree.children.map(update); console.log(tree); .as-console-wrapper { max-height: 100% !important; top: 0; }Un enfoque similar al de Nina y Jamie, pero escrito en un estilo de codificación algo diferente:
const transform = ({type, children = [], source_url, ...rest}) => type == 'social_embed' ? {type:'p', children: [{text: source_url}]} : {type, ...rest, ...(children.length ? {children : children .map (transform)} : {})} const input = {children: [{type: "p", children: [{text: ""}]}, {type: "social_embed", children: [{text: ""}], source_url: "some_url"}, {type: "p", children: [{type: "p", children: [{type: "p", children: [{text: ""}]}]}]}]} console .log (transform (input)) .as-console-wrapper {max-height: 100% !important; top: 0}