Skip to content

JavaScript算法练习 #6

@Yeahax

Description

@Yeahax

今天上班没事做,练习了一下算法

递归

       var data = [1, 2, [3, 4, [5]], 6, [7, [8, 9, [10, 11, [12]]]]];

        window.onload = function() {
            //递归
            var result = []
            fillArray(data, result)
            console.log(result)
        }
        //递归
        function fillArray(array, result) {
            array.forEach(function(i) {
                if (i instanceof Array) {
                    fillArray(i, result)
                } else {
                    result.push(i)
                }
            })
        }

ES5数组去重

    
        function delRepeat(array) {
            var result = []
            array.forEach(function(i, k) {
                if (result.indexOf(i) == -1) {
                    result.push(i)
                }
            })
            return result
        }

ES6数组去重

        function delRepeat(items) {
              return [...new Set(items)];
        }

获取m到n的随机数

       function fn(m,n){
             return parseInt(Math.random()*(n-m))+m;
       }

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions