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!
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);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))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))