打字稿中的嵌套过滤器

发布于 2025-01-10 11:19:37 字数 1066 浏览 1 评论 0原文

我有一个 JSON 数组,如下所示。

[
  {
        id: 1,
        name: 'Alex',
        activity: [
           {
                id: 'A1',
                status: true
            },

            {
                id: 'A2',
                status: true
            },

            {
                id: 'A3',
                status: false
            }

        ]
    },
    {
        id: 2,
        name: 'John',
        activity: [
            {
                id: 'A6',
                status: true
            },

            {
                id: 'A8',
                status: false
            },

            {
                id: 'A7',
                status: false
            }

        ]
    }
]

我想要获取一个状态为 true 的活动 id 数组。我可以使用nester forforEach 循环来实现这一点。但在这里,我希望借助 filtermapsome 等数组函数来实现。

我已经尝试过以下操作。

let newArr=arr.filter(a=> a.activity.filter(b=> b.status).map(c=> c.id))

但我没有得到正确答案 预期输出

['A1','A2','A6']

I have a JSON array, which looks as follows.

[
  {
        id: 1,
        name: 'Alex',
        activity: [
           {
                id: 'A1',
                status: true
            },

            {
                id: 'A2',
                status: true
            },

            {
                id: 'A3',
                status: false
            }

        ]
    },
    {
        id: 2,
        name: 'John',
        activity: [
            {
                id: 'A6',
                status: true
            },

            {
                id: 'A8',
                status: false
            },

            {
                id: 'A7',
                status: false
            }

        ]
    }
]

I want to get an array of activity id whose status should be true.I can achieve this with nester for or forEach loop. But here I am looking to achieve with the help of array functions like filter, map, and some.

I have already tried with the following.

let newArr=arr.filter(a=> a.activity.filter(b=> b.status).map(c=> c.id))

But I didn't get the correct answer
Expected output

['A1','A2','A6']

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

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

发布评论

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

评论(3

一人独醉 2025-01-17 11:19:37
function filter_activity(activities) {
    return activities
        && activities.length
        && activities.map(x => x.activity)
            .flat().filter(activity => activity.status)
            .map(x => x.id) || [];
}

插图

function filter_activity(activities) {
  return activities &&
    activities.length &&
    activities.map(x => x.activity)
    .flat().filter(activity => activity.status)
    .map(x => x.id) || [];
}
const input = [{
    id: 1,
    name: 'Alex',
    activity: [{
        id: 'A1',
        status: true
      },
      {
        id: 'A2',
        status: true
      },
      {
        id: 'A3',
        status: false
      }
    ]
  },
  {
    id: 2,
    name: 'John',
    activity: [{
        id: 'A6',
        status: true
      },
      {
        id: 'A8',
        status: false
      },
      {
        id: 'A7',
        status: false
      }
    ]
  }
];
console.log(filter_activity(input));


所见即所得 => 所见即所得

function filter_activity(activities) {
    return activities
        && activities.length
        && activities.map(x => x.activity)
            .flat().filter(activity => activity.status)
            .map(x => x.id) || [];
}

Illustration

function filter_activity(activities) {
  return activities &&
    activities.length &&
    activities.map(x => x.activity)
    .flat().filter(activity => activity.status)
    .map(x => x.id) || [];
}
const input = [{
    id: 1,
    name: 'Alex',
    activity: [{
        id: 'A1',
        status: true
      },
      {
        id: 'A2',
        status: true
      },
      {
        id: 'A3',
        status: false
      }
    ]
  },
  {
    id: 2,
    name: 'John',
    activity: [{
        id: 'A6',
        status: true
      },
      {
        id: 'A8',
        status: false
      },
      {
        id: 'A7',
        status: false
      }
    ]
  }
];
console.log(filter_activity(input));


WYSIWYG => WHAT YOU SHOW IS WHAT YOU GET

难如初 2025-01-17 11:19:37
let arr = json.flatMap(e => e.activity.filter(el => el.status).map(el => el.id))
let arr = json.flatMap(e => e.activity.filter(el => el.status).map(el => el.id))
幸福%小乖 2025-01-17 11:19:37
let newArr=arr.map(x => x.activity)
            .reduce((acc, val) => acc.concat(val), [])
            .filter((activity:any) => activity.status)
            .map((x:any) => x.id) || [];

我在使用 flat()flatMap() 时出错。因此,我使用了 reduce()

let newArr=arr.map(x => x.activity)
            .reduce((acc, val) => acc.concat(val), [])
            .filter((activity:any) => activity.status)
            .map((x:any) => x.id) || [];

I got error when using flat() and flatMap().So, I have used reduce().

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