Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

132
Vistas
Recursively get nested array which status equal one

I've a nested array below,in which should match the status === 1,then return ids ,expect will be an array like this [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: []
            }
        ]
    }
]

Any help would be much appreciated!

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

You could take a recursive approach and return the id, if status matches and the other id of the children.

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 Denunciar

0

This will do the trick

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 Denunciar

0

This should perform well.

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda