You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在JS/TS中统计对象数组内嵌套对象id等于parent_user_id的数量?

问题描述

我有一个包含嵌套对象数组的对象数组,结构如下:

// Code Snippet 1
[
{
    "name" : "A",
    "description" : null,
    "users" : [
        {
            "id" : "63a8808c52f40e1d48a3d1da",
            "owner" : "John Doe",
            "purchase_date" : "2022-12-25",
            "status" : "active",
            "parent_user_id" : "63a8808c52f40e1d48a3d1da",
            "recent_items": ["tomato","onion"]
        },
        {
            "id" : "63a880a552f40e1d48a3d1dc",
            "owner" : "John Doe 1",
            "purchase_date" : "2022-12-25",
            "parent_user_id" : "63a8808c52f40e1d48a3d1da",
            "recent_items": ["onion"]
        },
        {
            "id" : "63a880f752f40e1d48assddd",
            "owner" : "John Doe 2",
            "purchase_date" : "2022-12-25",
            "parent_user_id" : "63a8808c52f40e1d48a3d1da",
        },
        {
            "id" : "63a880f752f40e1d48a3d207",
            "owner" : "John Doe 11",
            "dt" : "2022-12-25",
            "status" : "inactive",
            "parent_user_id" : "63a880f752f40e1d48a3d207",
        },
        {
            "id" : "63a880f752f40e1d48agfmmb",
            "owner" : "John Doe 112",
            "dt" : "2022-12-25",
            "status" : "active",
            "parent_user_id" : "63a880f752f40e1d48agfmmb",
            "recent_items": ["tomato"]
        },
        {
            "id" : "63a880f752f40e1d48agggg",
            "owner" : "John SS",
            "dt" : "2022-12-25",
            "status" : "inactive",
            "parent_user_id" : "63a880f752f40e1d48agggg",
        },
        {
            "id" : "63a880f752f40e1d487777",
            "owner" : "John SS",
            "dt" : "2022-12-25",
            "parent_user_id" : "63a880f752f40e1d48agggg",
        }
    ]
}
]

需求是统计所有嵌套的users数组中,parent_user_id等于id的记录总数(示例预期结果为4),希望用更简洁的方式替代多层for循环。

简洁实现方式

方法1:使用reduce链式处理

通过reduce遍历外层数组,累加每个外层对象中符合条件的用户数量:

const data = [/* 你的数组数据 */];

const count = data.reduce((total, item) => {
  return total + item.users.filter(user => user.id === user.parent_user_id).length;
}, 0);

console.log(count); // 输出4

方法2:先扁平化再统计

先用flatMap提取所有嵌套的users数组,再统一过滤统计:

const data = [/* 你的数组数据 */];

const allUsers = data.flatMap(item => item.users);
const count = allUsers.filter(user => user.id === user.parent_user_id).length;

console.log(count); // 输出4

TypeScript版本(带类型定义)

如果用TypeScript,可以先定义类型确保类型安全:

interface User {
  id: string;
  parent_user_id: string;
  owner: string;
  purchase_date?: string;
  dt?: string;
  status?: string;
  recent_items?: string[];
}

interface Group {
  name: string;
  description: string | null;
  users: User[];
}

const data: Group[] = [/* 你的数组数据 */];

const count = data.reduce((total, group) => 
  total + group.users.filter(user => user.id === user.parent_user_id).length, 0
);

console.log(count); // 输出4

内容的提问来源于stack exchange,提问作者Dejavu

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.28 02:28:09