递归结构json取出所有id最大值

新手上路,请多包涵

怎么获取所有的value值,并取出最大值呢
数据引用递归结构的json取指定的值?

[
  {
    "label": "上海",
    "value": 310000,
    "children": [
      {
        "label": "上海",
        "value": 310100,
        "children": [
          {
            "label": "金山",
            "value": 310116,
            "children": [
              {
                "label": "测试123123",
                "value": 133
              }
            ]
          },
          {
            "label": "黄浦",
            "value": 310101,
            "children": [
              {
                "label": "测试播放系统",
                "value": 123
              },
              {
                "label": "DreamFitness",
                "value": 130
              }
            ]
          },
          {
            "label": "闵行",
            "value": 310112,
            "children": [
              {
                "label": "测试1",
                "value": 128
              },
              {
                "label": "纽绍客",
                "value": 129
              }
            ]
          },
          {
            "label": "浦东",
            "value": 310115,
            "children": [
              {
                "label": "兼容场馆",
                "value": 125
              }
            ]
          },
          {
            "label": "徐汇",
            "value": 310104,
            "children": [
              {
                "label": "美奂旗舰店",
                "value": 122
              },
              {
                "label": "开发测试",
                "value": 124
              },
              {
                "label": "欣欣向荣",
                "value": 126
              },
              {
                "label": "魏叶的场馆",
                "value": 127
              },
              {
                "label": "测试",
                "value": 131
              },
              {
                "label": "测试2",
                "value": 132
              }
            ]
          }
        ]
      }
    ]
  }
]
阅读 2.6k
4 个回答

你得问问后端老哥
省市区这等类型的区域数据存储,现在基本都是单条记录,分表或单表存储的
也就是说接口返回给你,是原本一维的数据做了group分组的

不方便的话,自己顶层排个序,取末尾的最深层最大值吧

let maxNum = 0; //最大的value
const vals = [] //所有的value
    const fun = arr => {
      arr.forEach(item => {
        if (item.value > maxNum) maxNum = item.value;
        vals.push(item.value)
        if (item.children) fun(item.children);
      });
    };
    fun([{value:1},{value:3}]);

找到一个做法,外部传入空数组,但不懂为什么

generateIdList(menus, res) {
  for (const menu of menus) {
    const { resId, children } = menu
    res.push(resId)
    if (children) {
      res = generateIdList(children, res)
    }
  }
  return res
}
generateIdList(data, [])
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题