And an array of arrays class

Array

- definition of array
literal;
var new new ARR = the Array (length / Content)

- Read and write the array
can not read overflow, returns undefined
but may overflow write, the middle is empty, (after this operation should be an array of class! - need to verify personal views!!)
Here Insert Picture Description
Here Insert Picture Description
ECMAScript is the DOM BOM js three parts!
Different browsers have differences in the DOM and BOM! In particular BOM! Operation of the browser BOM!
ES3.0 es5.0 es6.0

  • An array of several methods commonly used!
    - change the original array
    7 Ways: the Push, POP, unshift, the Shift, splice, Reverse, the Sort
  • Source array does not change
    - the concat, the Join <-> Split (STR), toString, Slice

- push: an array final add


Here Insert Picture Description
Returns the length of the array!
push method of rewriting:

var arr = [1,2,3];
        Array.prototype.push = function(){
            for(var i = 0 ;i < arguments.length;i++){
                this[this.length] = arguments[i];
            }
            return this.length;
        }

- pop: Cut the last array, mass participation with no eggs


Insert pictures described herein
value or length of the returned array!
pop method overrides:

var arr = [1, 2, 3];
        Array.prototype.pop = function() {
            var popValue = this[this.length - 1];
            this.length--;
            return popValue;
        };

- shift unshift: Save and processing at the beginning of an array


Here Insert Picture Description
unshift add the required parameters! shift delete unnecessary parameters!
unshift method overrides:

var arr = [1, 2, 3];
        Array.prototype.unshift = function() {
            var len = arguments.length,
                arg = [];
            for (var j = 0; j < len; j++) {
                arg[j] = arguments[j];
            }
            for (var i = 0; i < this.length; i++) {
                arg[arg.len + i] = arg.push(this[i]);
            }
            return arg;
        };

This approach is not good! To return the length of the words, arr has not changed!


- reverse: reverse order


Method overrides:

var arr = [1, 2, 3];
        Array.prototype.reverse = function() {
            var len = this.length,
                revArr = [];
            for (var i = 0; i < len; i++) {
                revArr[i] = this[len - i - 1];
            }
            arr = revArr;
            return arr;
        };

Here Insert Picture Description


- splice: Slice (beginning from the first of several, length taken, adding new data incision)


Here Insert Picture Description
Adding a certain position, may be used to override the unshift
Here Insert Picture Description
parameter start bit is negative
Here Insert Picture Description


- sort: ascending sort code asc


Here Insert Picture Description
So the system left us with an interface, let us write your own sorting rules
Here Insert Picture Description

var arr = [1, 2, 6, 5, 20, 11];
        //必须有两个参数
        //看返回值,若返回值为负,那么前面的数放到前面
        //         若返回值为正,那么后面的数放到前面
        arr.sort(function(a, b) {
            return a - b;
            // return b-a;降序
        });

To an ordered array of out of order (ha ha!)

var arr = [1, 2, 5, 6, 11, 20];
        //必须有两个参数
        //看返回值,若返回值为负,那么前面的数放到前面
        //         若返回值为正,那么后面的数放到前面
        arr.sort(function(a, b) {
            return Math.random() - 0.5;
            // return b-a;降序
        });

Each refresh is different!
Here Insert Picture Description
Sort by some value target object

var obj = {
            age: 185
        };
        obj1 = {
            age: 195
        };
        obj2 = {
            age: 20
        };
        arr = [obj, obj1, obj2];
        //必须有两个参数
        //看返回值,若返回值为负,那么前面的数放到前面
        //         若返回值为正,那么后面的数放到前面
        arr.sort(function(a, b) {
            return a.age - b.age;
            // return b-a;降序
        });

Byte ordering:

function retBytes(str) {
            var num = str.length;
            for (var i = 0; i < str.length; i++) {
                if (str.charCodeAt(i) > 255) {
                    num++;
                }
            }
        }
        var arr = ["sfjslj", "a", "b", "c", "wangjia"];
        arr.sort(function(a, b) {
            return retBytes(a) - retBytes(b);
        });

Here Insert Picture Description


- concat: connecting two arrays (not alter the original array)


Here Insert Picture Description


-slice :( beginning from the first of several interception, interception to this bit)


Here Insert Picture Description
Note the difference between he and splice in!


-toString (): It do not say it!


Class array (an array of arrays can be used when the non-)

an array of arguments is a class
attribute to the index (digital) property, you must have a length, plus the best push,

Here Insert Picture Description
Plus after the splice method becomes an array
Here Insert Picture Description
Ali original title:

        var obj = {
            "2": "a",
            "3": "b",
            length: 2,
            push: Array.prototype.push
                // splice: Array.prototype.splice
        };
        obj.push("c");
        obj.push("d");

Here Insert Picture Description

var obj = {
            "0": "s",
            "1": "ss",
            "2": "a",
            "3": "b",
            length: 4,
            name: wangjia,
            age: 23,
            push: Array.prototype.push,
            splice: Array.prototype.splice
        };

Here Insert Picture Description

Published 37 original articles · won praise 0 · Views 696

Guess you like

Origin blog.csdn.net/weixin_43704007/article/details/105038461