Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

371
Views
Cómo definir el esquema para el modelo recursivo con Normalizr

Tiene un pequeño problema al tratar de normalizar una carga útil, que contiene un esquema anidado del mismo tipo que el padre que usa Normalizr

Por ejemplo, tengo el objeto inicial ( menu ) que tiene un hijo ( sections ) que es una matriz de objetos ( section ), que puede profundizar.

 { id: 123, sections: [{ id: 1, sections:[{ id: 4, sections: [ id: 5, sections: [] ] }] }, { id: 2, sections:[] }, { id: 3, sections:[] }] }

Comencé creando un esquema de menu , que tenía secciones en la definición que se vinculaban a un esquema de sections , que funcionaba para el primer paso, pero luego no manejaba elementos secundarios de las secciones, así que agregué una definición posterior dentro del esquema de section con el mismo nombre (valía la pena intentarlo) pero no funcionó.

 const section = new schema.Entity('sections') const sections = new schema.Entity('sections', { sections: section }) const menu = new schema.Entity('menu', { sections: [ sections ] }) section.define({ sections })

Espero terminar con el siguiente objeto:

 { entities: { menu: { sections: [1, 2, 3] }, sections: [{ 1: { id: 1, sections: [4] }, 2: { id: 2, sections: [] }, 3: { id: 3, sections: [] }, 4: { id: 4, sections: [5] }, 5: { id: 5, sections: [] }, }] } }
over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

Su esquema de sections debe ser un Array .

 const section = new schema.Entity('sections') const sections = new schema.Array(section); section.define({ sections }); const menu = new schema.Entity('menu', { sections });

Entonces, al usarlo...

 const data = { id: 123, sections: [{ id: 1, sections:[{ id: 4, sections: [ { id: 5, sections: [] } ] }] }, { id: 2, sections:[] }, { id: 3, sections:[] }] }; normalize(data, menu)

Volverá:

 { "entities": { "sections": { "1": { "id": 1, "sections": [ 4 ] }, "2": { "id": 2, "sections": [] }, "3": { "id": 3, "sections": [] }, "4": { "id": 4, "sections": [ 5 ] }, "5": { "id": 5, "sections": [] } }, "menu": { "123": { "id": 123, "sections": [ 1, 2, 3 ] } } }, "result": 123 }
over 4 years ago · Santiago Trujillo Report

0

Si alguien tiene el caso de objetos anidados del mismo "tipo", por ejemplo, "secciones" y la estructura de nivel superior también es una matriz de "secciones", como esta:

 const data = [ { id: 1, sections:[{ id: 4, sections: [ { id: 5, sections: [] } ] }] }, { id: 2, sections:[] }, { id: 3, sections:[] } ]

aquí una forma de "desanidarlos":

 import {schema, normalize} from "normalizr"; const child = new schema.Entity("sections"); const sections = new schema.Array(child); child.define({sections}); const topLevel = new schema.Entity("sections", { sections }); const customSchema = [topLevel]; console.log(normalize(data, customSchema));

Lo que obtendrás es:

 { "entities":{ "sections":{ "1":{ "id":1, "sections":[ 4 ] }, "2":{ "id":2, "sections":[ ] }, "3":{ "id":3, "sections":[ ] }, "4":{ "id":4, "sections":[ 5 ] }, "5":{ "id":5, "sections":[ ] } } }, "result":[ 1, 2, 3 ] }
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!