js数组两层遍历

11 篇文章 0 订阅
9 篇文章 0 订阅
<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>element-ui</title>
    <script src="https://cdn.jsdelivr.net/npm/vue@2.5.16/dist/vue.js"></script>
    <link rel="stylesheet" href="https://unpkg.com/element-ui/lib/theme-chalk/index.css">
    <script src="https://unpkg.com/element-ui/lib/index.js"></script>
</head>

<body>
    <div id="app">
    </div>
    <script>
        new Vue({
            el: "#app",
            data: {
                unlockType: [{
                        code: "A",
                        name1: "APP开门"
                    },
                    {
                        code: "F",
                        name1: "人脸识别"
                    },
                    {
                        code: "P",
                        name1: "语音对讲"
                    },
                    {
                        code: "C",
                        name1: "刷卡开门"
                    },
                    {
                        code: "V",
                        name1: "APP对讲"
                    },
                    {
                        code: "S",
                        name1: "物业开门"
                    },
                    {
                        code: "R",
                        name1: "人证开门"
                    },
                    {
                        code: "B",
                        name1: "小程序开门"
                    },
                    {
                        code: "H",
                        name1: "海X小程序开门"
                    },
                    {
                        code: "W",
                        name1: "手环开门"
                    },
                    {
                        code: "T",
                        name1: "二维码开门"
                    }
                ],

                list: [{
                    num: 0,
                    fdate: "2020-01-04",
                    unlockType: "APP开门",
                    unlockTypeId: "A",

                }, {
                    num: 0,
                    fdate: "2020-01-04",
                    unlockType: "小程序开门",
                    unlockTypeId: "B",

                }, {
                    num: 0,
                    fdate: "2020-01-04",
                    unlockType: "刷卡开门",
                    unlockTypeId: "C",

                }, {
                    num: 0,
                    fdate: "2020-01-04",
                    unlockType: "人脸识别",
                    unlockTypeId: "F",

                }, {
                    num: 0,
                    fdate: "2020-01-04",
                    unlockType: "海X小程序开门",
                    unlockTypeId: "H",

                }, {
                    num: 0,
                    fdate: "2020-01-04",
                    unlockType: "语音对讲",
                    unlockTypeId: "P",

                }, {
                    num: 0,
                    fdate: "2020-01-04",
                    unlockType: "人证开门",
                    unlockTypeId: "R",

                }, {
                    num: 0,
                    fdate: "2020-01-04",
                    unlockType: "物业开门",
                    unlockTypeId: "S",

                }, {
                    num: 0,
                    fdate: "2020-01-04",
                    unlockType: "二维码开门",
                    unlockTypeId: "T",

                }, {
                    num: 0,
                    fdate: "2020-01-04",
                    unlockType: "APP对讲",
                    unlockTypeId: "V",

                }, {
                    num: 0,
                    fdate: "2020-01-04",
                    unlockType: "手环开门",
                    unlockTypeId: "W",

                }, {
                    num: 0,
                    fdate: "2020-01-05",
                    unlockType: "APP开门",
                    unlockTypeId: "A",

                }, {
                    num: 0,
                    fdate: "2020-01-05",
                    unlockType: "小程序开门",
                    unlockTypeId: "B",

                }, {
                    num: 0,
                    fdate: "2020-01-05",
                    unlockType: "刷卡开门",
                    unlockTypeId: "C",

                }, {
                    num: 0,
                    fdate: "2020-01-05",
                    unlockType: "人脸识别",
                    unlockTypeId: "F",

                }, {
                    num: 0,
                    fdate: "2020-01-05",
                    unlockType: "海X小程序开门",
                    unlockTypeId: "H",

                }, {
                    num: 0,
                    fdate: "2020-01-05",
                    unlockType: "语音对讲",
                    unlockTypeId: "P",

                }, {
                    num: 0,
                    fdate: "2020-01-05",
                    unlockType: "人证开门",
                    unlockTypeId: "R",

                }, {
                    num: 0,
                    fdate: "2020-01-05",
                    unlockType: "物业开门",
                    unlockTypeId: "S",

                }, {
                    num: 0,
                    fdate: "2020-01-05",
                    unlockType: "二维码开门",
                    unlockTypeId: "T",

                }],
                series: []
            },
            created() {
                //第一次遍历
                this.unlockType.forEach((item, index) => {
                    //先在数组中建几个对象
                    this.series.push({
                        date: [],
                        data: []
                    });
                    //遍历里再遍历
                    this.list.forEach(i => {
                        //将合适
                        if (item.code === i.unlockTypeId) {
                            this.series[index].date.push(i.fdate);
                            this.series[index].data.push(i.num)
                        }
                    })
                });
                console.log(this.series);
            }
        })
    </script>
</body>

</html>

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
JavaScript中,map函数可以用于遍历多层数组。当数组的元素也是数组时,可以使用嵌套的map函数来实现遍历。以下是一个示例代码: ``` const arr = [[1, 2], [3, 4], [5, 6]]; const result = arr.map(innerArr => { return innerArr.map(num => num * 2); }); console.log(result); ``` 在上述中,我们有一个包含多个内部数组的外部数组。通过使用map函数,我们可以遍历外部数组的每个内部数组。然后,对于每个内部数组,我们再次使用map函数来遍历其中的每个元素,并将其乘以2。最终,我们得到一个新的数组result,其中包含原始数组中每个元素的两倍值。 所以,通过嵌套的map函数,我们可以非常方便地遍历多层数组。这样可以在每个元素上执行自定义的操作,并返回一个新的数组作为结果。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [js用递归遍历多维数组_js面试题更新之40](https://blog.csdn.net/weixin_39634132/article/details/111050345)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* *3* [js用递归遍历多维数组_JS数组遍历上 (含forEach等方法源码)](https://blog.csdn.net/weixin_39812577/article/details/110287876)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值