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

133
Views
Obtiene recursivamente una matriz anidada cuyo estado es igual a uno

Tengo una matriz anidada a continuación, en la que debería coincidir con el estado === 1, luego devolver los identificadores, espero que sea una matriz como esta [1,3,9,11]

 const data = [ { title: 'level1', id: 1, status:1, children: [ { title: 'level1-1', id: 3, status:1, children: [ { title: 'level1-1-1', id: 7, field: '', status:0 }, { title: 'level1-1-2', id: 9, field: '', status:1 } ] } ] }, { title: 'level2', id: 11, status:1, children: [ { title: 'level2-1', id: 12, status:0, children: [] } ] } ]

¡Cualquier ayuda sería muy apreciada!

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

0

Puede adoptar un enfoque recursivo y devolver la identificación, si el status coincide con la otra id de los niños.

 const getIds = ({ id, status, children = [] }) => [ ...(status === 1 ? [id] : []), ...children.flatMap(getIds) ], data = [{ title: 'level1', id: 1, status: 1, children: [{ title: 'level1-1', id: 3, status: 1, children: [{ title: 'level1-1-1', id: 7, field: '', status: 0 }, { title: 'level1-1-2', id: 9, field: '', status: 1 }] }] }, { title: 'level2', id: 11, status: 1, children: [{ title: 'level2-1', id: 12, status: 0, children: [] }] }], result = data.flatMap(getIds); console.log(...result);

about 4 years ago · Juan Pablo Isaza Report

0

Esto hará el truco

 const findWithStatus = statusToMatch => data => data.reduce((res, {id, status, children}) => { res = status === statusToMatch? [...res, id]: res if(!children) { return res } return [...res, ...(findWithStatus(statusToMatch)(children) || [])] }, []) const findWithStatus1 = findWithStatus(1) const data = [{ title: 'level1', id: 1, status: 1, children: [{ title: 'level1-1', id: 3, status: 1, children: [{ title: 'level1-1-1', id: 7, field: '', status: 0 }, { title: 'level1-1-2', id: 9, field: '', status: 1 } ] }] }, { title: 'level2', id: 11, status: 1, children: [{ title: 'level2-1', id: 12, status: 0 }] } ] console.log(findWithStatus1(data))

about 4 years ago · Juan Pablo Isaza Report

0

Esto debería funcionar bien.

 const data = [{title: 'level1',id: 1,status:1,children: [{title: 'level1-1', id: 3,status:1,children: [{ title: 'level1-1-1', id: 7, field: '', status:0 }, { title: 'level1-1-2', id: 9, field: '', status:1 } ]}]},{title: 'level2',id: 11,status:1,children: [{title: 'level2-1', id: 12,status:0,children: []}]}]; /** * Function that accpets two params * @param {Array} `obj` data to iterate. * @param {Number} `status` status you want to match. */ const getIdsWithStatus = (obj, status) => // Use the reduce function to create an array of ids from // the `obj` array obj.reduce((acc, item) => { // If the `obj` array has children in an item, // let's iterate the children, and push the result into the accumulator if (item.children) acc.push(...getIdsWithStatus(item.children, status)) // If the current item matches the target status, // push the item id if (item.status === status) acc.push(item.id) // Return accumulator return acc; }, []); console.log(getIdsWithStatus(data, 1))

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!