Javascript Crusoe (on) __ array, array-

Disclaimer: This article is a blogger original, reproduced, please attach a blog link. https://blog.csdn.net/AquamanTrident/article/details/90666740

First, the array:
       1, defined by:
            array literal var arr = [1,2,3,4,5];
            constructor var arr = new Array (1,2,3,4,5) ;
            both are different as var arr1 = new array (10) ; a sparse array of length 10.
       2, read and write the array:
            the array is a special object, the overflow can be read, no read value returns undefined.

       3, the method of the array    
             1 changes the original array:
                 var ARR = [];
                 arr.push (666,666) at the end of the array was added
                 arr.pop () cut off the last array
                 arr.shift () array from the front shear
                 arr.unshift () added in front of the array
                 arr.reverse () reverse
                 arr.splice (iIndex, [iHowmany], [item1], [item2]), very important
                 arr.splice (1,2,3,4) from a beginning, taken two, placed at the cuts 3,4
                 arr.splice (-1,2) starting from the penultimate one, two, taken Bai
                 arr.sort () method of the array carry handle, good master, expand


                 Violence simulation push method:

        var arr = [];
        Array.prototype.push = function(){//没法传参数,不定数量的
            for(var i = 0; i < arguments.length; i++){
                this[this.length] = arguments[i];
            }
            return this.length;            
        }

                  Damn sort method:

        var arr = [1,3,2,10,9,7,5];
        console.log(arr.sort());//[1, 10, 2, 3, 5, 7, 9],什么情况

                 Optimization sort method:
                         1. The parameter must be written both
                         2. See Return Value
                                   1) A negative return value, the number in front of the front discharge
                                   2) is positive, set position, behind the front number
                                   3) is 0, fixed

        var arr = [1,3,2,10,9,7,5];
        arr.sort(function(a,b){//函数会被多次调用,冒泡排序的方式比较.
            // if(a > b){
            //     return 1;
            // }else{
            //     return -1;
            // }
            return a-b;//由上面推导得出: returna-b;升序 ,return b-a;降序!!!
        });
        console.log(arr);

                  sort expand:   

                  To an ordered array, out of order:

        var arr = [1,2,3,4,5,6,7];
        arr.sort(function(){
            return Math.random() - 0.5;
        });
        console.log(arr);

                  By age ascending order:

        var zhangFei = {name : 'zhangfei', age : 18}
        var liKui = {name : 'likui', age : 15}
        var guanYu = {name : 'guanyu', age : 20}
        var arr = [zhangFei, liKui, guanYu];
        arr.sort(function(a,b){
            return a.age - b.age;
        })
        console.log(arr);

              2 without changing the original array:
                  arr1.concat (arr2 is)
                  toString, before the details Now note
                  var newArr = arr.slice (iStart, iEnd ) taken from the start bit, taken to the bit (not including the bit)
                  arr.join ( '-'), in accordance with the transmission parameters (strings, strings are what lines), each connected to an array and returns a string
                  str.split ( '-'), it is a method of string, and join reciprocal , in accordance with the split parameters passed, it returns an array

                  Connecting together the following strings:

        var str1 = 'Alibaba',
            str2 = 'Tencent',
            str3 = 'Baidu',
            str4 = 'Sina',
            str5 = 'Meituan'
        var arr = [str, str1, str2, str3, str4, str5];
        console.log(arr.join(''));

                  Deduplication array (programmed on prototype chain):

        Array.prototype.unique = function(){
            var temp = {},//数组的每一位当做对象的属性名,随便赋给值
                newArr = [],
                len = this.length;
            for(var i = 0; i < len; i++){
                if(!temp[this[i]]){
                    temp[this[i]] = 'abc';
                    newArr.push(this[i]);
                }
            }
            return newArr;
        }

                  Deduplication string (programmed on prototype chain): the string into an array

        String.prototype.unique = function(){
            var arr = this.split('');
                temp = {},//数组的每一位当做对象的属性名,随便赋给值
                newArr = [];
                len = arr.length;
            for(var i = 0; i < len; i++){
                if(!temp[arr[i]]){
                    temp[arr[i]] = 'abc';
                    newArr.push(arr[i]);
                }
            }
            return newArr.join('');
        }

                  The first string to find the letters appear only once:

        function FirstNoRepeatLetter(str){
            var arr = [];
            for(var i = 0; i < str.length; i++){
                if(arr[str[i]] == undefined){
                    arr[str[i]] = 1;//arr['p'] = 1,arr['o'] = 1
                }else{
                    arr[str[i]]++;//arr['p'] = 2,arr['o'] = 2
                }
            }
            console.log(arr);
            for(var j = 0; j < str.length; j++){
                if(arr[str[j]] == 1){
                    console.log(j);
                    return j;
                }
            }
        }
        FirstNoRepeatLetter('mnbvcfxzxcvubnm,mnbvcxcvbnm');


Second, the array type (e.g., arguments, DOM, etc.)
       property of the index attribute, attribute length must be! ! ! The best plus push.
 

	var obj = {
            0 : 'a',
	    1 : 'b',
	    2 : 'c',
	    length : 3,
	    push : Array.prototype.push,//理解push内部原理
	    splice : Array.prototype.splice	
	}
	obj.push('d');

        Internal push principle:

        Array.prototype.push = function(target){
            this[this.length] = target;//此时的this是obj
            this.length++;    
        }

        Ali contempt questions:

        var obj = {
            2 : 'a',
            3 : 'b',
            length : 2,//决定了push到哪个位置
            push : Array.prototype.push//理解push内部原理
        }
        obj.push('c');
        obj.push('d');

The above content is a brother original, finishing from " crossing a Javascript education curriculum ", a recommended " crossing an education ."

Guess you like

Origin blog.csdn.net/AquamanTrident/article/details/90666740