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!
CodePudding user response:
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))
CodePudding user response:
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))
CodePudding user response:
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);