• JS遍历的一些测试


    测试代码

    <template>
        <div class="container">
            <span>This Test</span>
        </div>
    </template>
    
    <script>
        export default {
            name: "Test",
            data() {
                return {
                    arr: [{
                        a: 111,
                        b: 222
                    }, {
                        c: 333,
                        d: 444
                    }]
                }
            },
            mounted() {
                console.log(this.arrfor())
                console.log(this.arrforof())
                this.arrfilter()
                this.arrsome()
                console.log(this.arrforeach())
                console.log(this.arrmap())
                console.log(this.arrfind())
            },
            methods: {
                arrfor() {
                    var len = this.arr.length
                    for (var j = 0; j < len; j++) {
                        if (this.arr[j].a == 111) {
                            return 'arrfor success'
                        }
                    }
                },
                arrforof() {
                    for (var item of this.arr) {
                        if (item.a == 111) {
                            return 'forof success'
                        }
                    }
                },
                arrfilter() {
                    console.log(this.arr.filter(item => {
                        return item.c == 333
                    }))
                },
                arrsome() {
                    console.log(this.arr.some(item => {
                        if (item.a == 111) {
                            return 'arrsome success'
                        }
                    }))
                },
                arrforeach() {
                    this.arr.forEach(item => {
                        if (item.a == 111) {
                            return 'arrforeach success'
                        }
                    })
                },
                arrmap() {
                    this.arr.map(item => {
                        if (item.a == 111) {
                            return 'map success'
                        }
                    })
                },
    
                arrfind() {
                    this.arr.find((item) => {
                        item.a == 111
                    })
                }
    
            },
        }
    </script>
    
    <style scoped>
    
    </style>

    测试结果

    仅for和forof能正确return

    filter返回新数组,判断结果为true则加入新数组,否则过滤

    some返回true时跳出整个循环 ruturn 类型仅能为 Boolean

  • 相关阅读:
    hbase权威指南PDF下载
    流畅的pythonPDF下载
    现代操作系统PDF下载
    Kafka权威指南PDF下载
    java核心技术卷1PDF下载
    计算机网络自顶向下方法PDF下载
    git gitlab 部署
    MySQL 索引、事务与存储引擎(详细解读)
    QT: Layout布局间消除间隙(修改layout内置参数)
    数据结构---归并排序
  • 原文地址:https://www.cnblogs.com/xuanjiange/p/15306817.html
Copyright © 2020-2023  润新知