递归获得嵌套数组哪个状态等于一个

发布于 2025-01-24 01:35:25 字数 766 浏览 0 评论 0原文

我在下面有一个嵌套的数组,其中应与状态== 1,然后返回ID,预计将是这样的阵列[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: []
            }
        ]
    }
]

任何帮助都将不胜感激!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

十六岁半 2025-01-31 01:35:25

您可以采用递归方法并返回ID,如果状态匹配以及儿童的其他ID

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

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

柳若烟 2025-01-31 01:35:25

这应该表现良好。

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

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

吹泡泡o 2025-01-31 01:35:25

这将解决问题

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文