preface

  • Because usually in the process of writing code, some algorithms will often repeat, such as array to duplicate, array extraction random value! It’s not that hard of logic, but it takes some time to figure it out when you first encounter a requirement, so this document records the ideas of these common algorithms so that you won’t be overwhelmed the next time you encounter them!

  • This document does not consider es6 syntax, nor does it consider some of the filtering methods that come with Array!

Array to heavy

  • We do not consider a native filtering algorithm on the array, such as map, filter, etc.! The key to array de-duplication is that you need an intermediate array to store the array to help with array de-duplication!

  • Method one:

Var arr =,2,3,1,1,1,1 [1]; Function toHeavy(array){function toHeavy(array){var cache = {}; Var cacheArr = []; var cacheArr = []; for(var i = 0,len = array.length; i<len; i++){ if(! cache[array[i]]){ cacheArr.push(array[i]); cache[array[i]] = array[i]; }; }; return cacheArr; }; arr = toHeavy(arr); / / arr = = [1, 2, 3]Copy the code
  • Method 2:
Var arr = [1,2,3,1,1,1,1,1]; function toHeavy(array){ var cache = []; for(var i = 0,len = array.length; i<len; I++){function(){var isHeavy = false;} function(){var isHeavy = false; for(var j = 0,_len = cache.length; j<_len; j++){ if(cache[j] == array[i]){ isHeavy = true; break; }; }; if(! Cache.push (array[I]) {// If the current value is not repeated, then execute cache.push(array[I]); }; }) (); }; return cache; }; arr = toHeavy(arr);Copy the code

Finally, the real data will not be so simple, it may be a slightly more complex data, you should not be scared to give these arrays to the weight, in fact, the principle is the same, but you are confused!

A random selection of values in an array

  • The key to this algorithm is to use math.random.
Var arr = [' Ming ', 'red', 'Chen', 'less than', 'xiao LAN', 'little method', 'small p', 'zhang', 'town', 'wang', 'idiot', 'new force]; Function getArr(num,array){var aLength = array.length; if(num>=aLength){ num = aLength; }; var cacheArr = []; // Remember to assign directly, because an array is a reference, OriginArr = array.slice() var originArr = (function(){var arr = []; for(var i = 0,len = array.length; i<len; i++){ arr.push(array[i]); }; return arr; }) (); for(var i = 0; i<num; I++){//array.length cannot be written as aLength because aLength is a fixed value, Math.random() * array.length returns a value between the length and zero, including zero but not the length. Var _index = math.floor (math.random () * array.length); var _index = math.floor (math.random () * array.length); cacheArr.push(array[_index]); Array.splice (_index,1); array.splice(_index,1); }; Array = originArr; console.log(array); return cacheArr; }; var brr = getArr(5,arr);Copy the code

Gets an array of the letters of an interval

  • Two main methods apply here, charCodeAt for a String and fromCharCode for a static method on a String. The main idea is: first get the numeric representation of the beginning letter and the end letter of this interval, and then you can do a loop in this interval, and get the numeric representation of the letters of this interval, and finally call the numbers into letters and push them back into the array. Directly on the code:
    function getArrForAlphabet(startLetter,endLetter){
        //var regExp = /^[a-zA-Z]$/gi;
        var regExp = new RegExp("^[a-zA-Z]$");
        if(!regExp.test(startLetter) || !regExp.test(endLetter)){
            //console.log(regExp.test(startLetter));
            //console.log(regExp.test(endLetter));
            console.log('请传入字母!');
            return false;
        };
        //i是得到开始字母的数字表示,j得到结束字母的数字表示
        var i = startLetter.charCodeAt(0),j = endLetter.charCodeAt(0);
        //定义一个数组用于取出将来的字母
        var arr = [];
        //这里取<=符号是因为要取出结束的字母
        for(;i<=j;i++){ 
            //fromCharCode是String上的一个静态方法,用于将一个数字转换成对应的字母
            var letter = String.fromCharCode(i);
            arr.push(letter);
        };
        //记得最后返回arr
        return arr;
    };Copy the code