账号密码登录
微信安全登录
微信扫描二维码登录

登录后绑定QQ、微信即可实现信息互通

手机验证码登录
找回密码返回
邮箱找回 手机找回
注册账号返回
其他登录方式
分享
  • 收藏
    X
    如何获取树形JSON数组中的所有ID?以数组形式返回
    20
    0

    1.如何获取树形JSON数组中的所有ID?以数组形式返回
    2.`

    {
      "created": 2018,
      "updated": 2018,
      "id": "1",
      "orgName": "全部",
      "parentId": "0",
      "sort": 0,
      "type": "0",
      "channel": 1,
      "hasChild": true,
      "sign": false,
      "nextList": null,
      "childList": [
        {
          "created": 2018,
          "updated": 2018,
          "id": "2",
          "orgName": "1",
          "parentId": "1",
          "sort": 0,
          "type": null,
          "channel": null,
          "hasChild": true,
          "sign": false,
          "nextList": null,
          "childList": [
            {
              "created": 2018,
              "updated": 2018,
              "id": "27",
              "orgName": "11",
              "parentId": "2",
              "sort": 0,
              "type": null,
              "channel": null,
              "hasChild": true,
              "sign": false,
              "nextList": null,
              "childList": [
                {
                  "created": 2018,
                  "updated": 2018,
                  "id": "28",
                  "orgName": "111",
                  "parentId": "27",
                  "sort": 0,
                  "type": null,
                  "channel": null,
                  "hasChild": false,
                  "sign": false,
                  "nextList": null,
                  "childList": null,
                  "name": null,
                  "previousId": "0",
                  "hasNext": false,
                  "choice": "40_none",
                  "countLeaf": 0,
                  "nodeType": "parent_organization_dto"
                }
              ],
              "name": null,
              "previousId": "0",
              "hasNext": false,
              "choice": "40_none",
              "countLeaf": 0,
              "nodeType": "parent_organization_dto"
            }
          ],
          "name": null,
          "previousId": "0",
          "hasNext": false,
          "choice": "40_none",
          "countLeaf": 0,
          "nodeType": "parent_organization_dto"
        },
        {
          "created": 2018,
          "updated": 2018,
          "id": "3",
          "orgName": "2",
          "parentId": "1",
          "sort": 0,
          "type": null,
          "channel": null,
          "hasChild": false,
          "sign": false,
          "nextList": null,
          "childList": null,
          "name": null,
          "previousId": "0",
          "hasNext": false,
          "choice": "40_none",
          "countLeaf": 0,
          "nodeType": "parent_organization_dto"
        },
        {
          "created": 2018,
          "updated": 2018,
          "id": "4",
          "orgName": "3",
          "parentId": "1",
          "sort": 0,
          "type": null,
          "channel": null,
          "hasChild": false,
          "sign": false,
          "nextList": null,
          "childList": null,
          "name": null,
          "previousId": "0",
          "hasNext": false,
          "choice": "40_none",
          "countLeaf": 0,
          "nodeType": "parent_organization_dto"
        },
        {
          "created": 2018,
          "updated": 2018,
          "id": "5",
          "orgName": "4",
          "parentId": "1",
          "sort": 0,
          "type": null,
          "channel": null,
          "hasChild": false,
          "sign": false,
          "nextList": null,
          "childList": null,
          "name": null,
          "previousId": "0",
          "hasNext": false,
          "choice": "40_none",
          "countLeaf": 0,
          "nodeType": "parent_organization_dto"
        },
        {
          "created": 2018,
          "updated": 2018,
          "id": "6",
          "orgName": "5",
          "parentId": "1",
          "sort": 0,
          "type": null,
          "channel": null,
          "hasChild": false,
          "sign": false,
          "nextList": null,
          "childList": null,
          "name": null,
          "previousId": "0",
          "hasNext": false,
          "choice": "40_none",
          "countLeaf": 0,
          "nodeType": "parent_organization_dto"
        }
      ],
      "name": null,
      "previousId": "0",
      "hasNext": false,
      "choice": "40_none",
      "countLeaf": 0,
      "nodeType": "parent_organization_dto"
    }

    3.预期

    [1,2,27,28,3,4,5,6]
    0
    打赏
    收藏
    点击回答
        全部回答
    • 0
    • £九霄★战魂彡 普通会员 1楼

      在JavaScript中,你可以使用for...in循环和JSON.parse()方法来获取树形JSON数组中的所有ID。以下是一个示例:

      ```javascript function getIdsFromJson(jsonArray) { var ids = []; for (var i = 0; i < jsonArray.length; i++) { var id = jsonArray[i].id; ids.push(id); } return ids; }

      var jsonArray = [ { id: 1, children: [ { id: 2 }, { id: 3 } ] }, { id: 4, children: [ { id: 5 }, { id: 6 } ] } ];

      console.log(getIdsFromJson(jsonArray)); // 输出: [1, 2, 3, 4, 5, 6] ```

      在这个示例中,我们首先创建了一个名为getIdsFromJson的函数,它接受一个JSON数组作为参数。然后,我们使用一个for...in循环遍历数组中的每个元素。对于每个元素,我们获取其id属性,然后将这个属性添加到ids数组中。最后,我们返回ids数组。

    更多回答
    扫一扫访问手机版
    • 回到顶部
    • 回到顶部