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

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

手机验证码登录
找回密码返回
邮箱找回 手机找回
注册账号返回
其他登录方式
分享
  • 收藏
    X
    es6 数组问题
    23
    0
            "showModule": [{
                "action_id": "21",
                "action_name": "待办任务",
                "action_type": "1",
                "order": "1",
                "pid": "0"
            }, {
                "action_id": "20",
                "action_name": "物业资源管理",
                "action_type": "1",
                "order": "2",
                "pid": "0",
                "son": [{
                    "action_id": "22",
                    "action_name": "楼栋管理",
                    "action_type": "2",
                    "order": "1",
                    "pid": "20",
                    "son": [{
                        "action_id": "23",
                        "action_name": "添加",
                        "action_type": "3",
                        "order": "1",
                        "pid": "22"
                    }, {
                        "action_id": "24",
                        "action_name": "修改",
                        "action_type": "3",
                        "order": "2",
                        "pid": "22"
                    }, {
                        "action_id": "25",
                        "action_name": "删除",
                        "action_type": "3",
                        "order": "4",
                        "pid": "22"
                    }, {
                        "action_id": "31",
                        "action_name": "详情",
                        "action_type": "3",
                        "order": "4",
                        "pid": "22"
                    }]
                }, {
                    "action_id": "26",
                    "action_name": "房间管理",
                    "action_type": "2",
                    "order": "2",
                    "pid": "20",
                    "son": [{
                        "action_id": "27",
                        "action_name": "添加",
                        "action_type": "3",
                        "order": "1",
                        "pid": "26"
                    }, {
                        "action_id": "28",
                        "action_name": "修改",
                        "action_type": "3",
                        "order": "2",
                        "pid": "26"
                    }, {
                        "action_id": "29",
                        "action_name": "删除",
                        "action_type": "3",
                        "order": "3",
                        "pid": "26"
                    }, {
                        "action_id": "30",
                        "action_name": "详情",
                        "action_type": "3",
                        "order": "4",
                        "pid": "26"
                    }]
                }],

    数据结构是这样的

    我想处理一下数据结构
    变成下面这样 ,应该怎么写呢

    showModule:{
      label: action_name,
      value: action_id,
        son:[
        {
        label: action_name,
          value: action_id,
          son:{
          label: action_name,
          value: action_id,
          }
        },
        {
        label: action_name,
          value: action_id
          }
        ]
    }

    这么写就能循环一级啊 在item.son 上用map就报错了

      const mapData = data.showModule.map(item => ({
          label: item.action_name,
          value: item.action_id,
          subModule: item.son
          }))
    0
    打赏
    收藏
    点击回答
        全部回答
    • 0
    • 島嶼是海的傷疤 普通会员 1楼

      在JavaScript中,数组是一种特殊的数据类型,它具有以下几个特点:

      1. 数组是一种有序的序列,它的元素是不可变的。
      2. 数组可以使用索引来访问其元素。
      3. 数组可以使用push()、pop()、shift()、unshift()、slice()、concat()、sort()等方法操作。

      下面是一些关于ES6数组问题的示例:

      1. 使用push()方法在数组末尾添加一个元素:

      javascript let arr = [1, 2, 3]; arr.push(4); console.log(arr); // 输出:[1, 2, 3, 4]

      1. 使用pop()方法删除数组的最后一个元素:

      javascript let arr = [1, 2, 3]; arr.pop(); console.log(arr); // 输出:[1, 2]

      1. 使用shift()方法删除数组的第一个元素:

      javascript let arr = [1, 2, 3]; arr.shift(); console.log(arr); // 输出:[2, 3]

      1. 使用unshift()方法在数组的开头添加一个元素:

      javascript let arr = [1, 2, 3]; arr.unshift(4); console.log(arr); // 输出:[4, 1, 2, 3]

      1. 使用slice()方法从数组中提取一部分元素:

      javascript let arr = [1, 2, 3]; let slicedArr = arr.slice(1, 4); console.log(slicedArr); // 输出:[2, 3]

      1. 使用sort()方法对数组进行排序:

      javascript let arr = [5, 3, 1, 4, 2]; arr.sort((a, b) => a - b); console.log(arr); // 输出:[1, 2, 3, 4, 5]

      1. 使用concat()方法将两个或更多的数组合并成一个:

      javascript let arr1 = [1, 2, 3]; let arr2 = [4, 5, 6]; let mergedArr = arr1.concat(arr2); console.log(mergedArr); // 输出:[1, 2, 3, 4, 5, 6]

      1. 使用sort()方法对数组进行分组并对每个分组进行排序:

      javascript let arr = [ { name: 'John', age: 20 }, { name: 'Jane', age: 25 }, { name: 'Bob', age: 30 }, { name: 'Alice', age: 35 } ]; arr.sort((a, b) => b.age - a.age); console.log(arr);

      1. 使用sort()方法对数组进行排序,还可以指定排序的规则:

      javascript let arr = [ { name: 'John', age: 20 }, { name: 'Jane', age: 25 }, { name: 'Bob', age: 30 }, { name: 'Alice', age: 35 } ]; arr.sort((a, b) => b.age - a.age, { reverse: true }); console.log(arr);

      1. 使用sort()方法对数组进行降序排序:

      javascript let arr = [ { name: 'John', age: 20 }, { name: 'Jane', age: 25 }, { name: 'Bob', age: 30 }, { name: 'Alice', age: 35 } ]; arr.sort((a, b) => b.age - a.age, { reverse: false }); console.log(arr);

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