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

200
Views
¿Hay alguna manera de reducir un json anidado extrayendo solo ciertos valores?

Tengo la siguiente estructura json anidada:

 { element: "a", items: ["l1", "l2"], children: [{ element: "b", children: [{ element: "c", items: ["l3"] }, { element: "b", child: { element: "c", items: ["l4"] } }

Básicamente:

  • Un "elemento" contiene un nombre, una lista de elementos y una lista de niños que son ellos mismos un "elemento".
  • La lista de elementos puede faltar

Me gustaría procesar este json para obtener una matriz plana que contenga todos los elementos:

 const finalArray = parse(json); //returns ["l1","l2","l3","l4"]

¿Hay alguna forma elegante de lograr esto con funciones de mapeo/filtro?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

 const getItems = (acc = [], { items = [], child = {}, children = [] }) => [ ...acc, ...items, ...(child?.items || []), ...(children.length ? children.reduce(getItems, acc) : []) ]; const data = { element:"a", items: ["l1","l2"], children: [ { element: "b", children: [ { element: "c", items: ["l3"] }, { element: "b", child: { element: "c", items: ["l4"] } } ] } ] } console.log( getItems([], data) );

about 4 years ago · Juan Pablo Isaza 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!