Array-like structure

1. Only integers that start from zero and increase naturally are used as key names. 2

Create an array

1.Array.of()

Array.of(element0[, element1[, …[, elementN]]])

Function: converts a set of parameters to an array instance

Parameters: elementN: Any of the parameters that will be elements in the returned array, in order.

// The method to create an array
        // array.of (set of arguments) -- converts a set of arguments to an Array instance
        Array._of = function (. args) {
           
            return args
        }
Copy the code

2.Array.from()

Array.from(arrayLike[, mapFn[, thisArg]])

Function: converts an array-like object/iterable to an array instance

Parameters:

ArrayLike: An array-like object or traversable object that you want to convert into a real array.

MapFn: An optional parameter. If this parameter is specified, the resulting array will be processed by this function before being returned.

ThisArg: Optional argument, the value of this when the mapFn function is executed.

// The method to create an array
        // array.form (array-like object/iterable) -- converts an array-like object/iterable into an Array instance
        // Array.from(arrayLike[, mapFn[, thisArg]])
        Array._from = function (object, mapfunction, thisvalue) {
            let objThis = thisvalue || this
            let result = []
            // Return an empty array if there is no length attribute or length 0
            if(! object.length) {return result
            }
            if (typeof object === 'string') {
                return object.split(' ')}else if (object instanceof  Array) {
                object.forEach(item= > {
                    result.push(item)
                })
            } else {
                Object.keys(object).forEach(key= > {
                    if(key ! ='length') {
                        result.push(object[key])
                    }
                })
            }
            if((typeofmapfunction ! = ='function') && ( typeofmapfunction ! = ='undefined' )) {
                throw new TypeError ( mapfunction + 'is not a function')}if ((typeofmapfunction ! = ='undefined')) {
                return result.map(mapfunction, objThis)
            }
            return result
        }

        // Array-like tests
         const eg1 = Array._from({0:0.1:1.2:2.3:4.5:'aa'.length:2})
         console.log(eg1);
        // String tests
         const eg3 = Array._from('asiwdygyqw')
         console.log(eg3);
        // Array test
         const eg22 = Array._from([1.2.3.9.8.7].x= > x*100)
         console.log(eg22);
Copy the code

How to change the array

1. arrayObject.splice()

arrayObject.splice(index,howmany,item1,….. ,itemX)

Function:

Delete – Pass two parameters: the start position and the number of delets

Insert – Pass three arguments: the start position, 0 (the number of elements to delete), and any number of elements to insert

Replace – Pass three arguments: the starting position, the number of elements to delete, and any number of elements to insert

Array.prototype._splice = function (index, howmany) {
            let arr = this
            // Cut the array before the position
            let beforeArr = arr.slice(0, index)
            // Cut the array after the position
            let afterArr = arr.slice(index+howmany, arr.length)
            // Intercept the added parameter
            let subArr = Array.prototype.slice.call(arguments.2)
            // The truncated array
            let returnArr = arr.slice(index,index+howmany)
            let result = []
            result = [...beforeArr,...subArr,...afterArr]
            // Change the original array
            for (let i=0; i<result.length; i++) {this[i] = result[i]
            }
            if (this.length-result.length) {
                this.splice(result.length,this.length-result.length)
            }
            // Return the truncated array
            return returnArr

        }
Copy the code

Array. The prototype. Slice. The call () method a: www.cnblogs.com/jing-tian/p…

2. arrayObject.sort()

// arrayObject.sort()
        Array.prototype._sort = function (func) {
            let arr = this
            // Return the array if its length is less than or equal to 1
            if (arr.length<=1) {
                return arr
            }
            // If there are no function arguments
            if (func == undefined) {
                // Bubble sort, two layers of cycle, the outer layer controls the number of trips, the inner layer controls the number of comparisons per trip
                for (let i=0; i<arr.length; i++) {for(let j=0; j<arr.length-i-1; j++) {let temp = ""
                        if(String(arr[j])>String(arr[j+1])) {
                            temp = arr[j+1]
                            arr[j+1] = arr[j]
                            arr[j] = temp
                        }
                    }
                }
            } else if (typeof func == 'function') {
                for(let i=0; i<arr.length; i++) {for (let j=0; j<arr.length-i-1; j++){let flag = func(arr[j],arr[j+1])
                        if(flag>0) {// This is also a form of exchange
                            arr[j] = arr[j] +arr[j+1]
                            arr[j+1] = arr[j] - arr[j+1]
                            arr[j] = arr[j] - arr[j+1]}}}}else {
                throw new TypeError (func + ' is not a function')}return arr
        }
        const arr1 = [1.5.4.1.5.3.7]
        arr1._sort((a,b) = > b-a)
        console.log(arr1);
Copy the code

3. arrayObject.pop()

// arrayobject.pop()
        Array.prototype._pop = function () {
            let arr = this
            // Return undefined if the array is empty
            if (arr.length == 0) {
                return undefined
            }
            let result = arr[arr.length-1]
            this.length = this.length-1
            return result
        }
        const arr1 = [1.55.7.88]
        console.log(arr1._pop());
Copy the code

4. arrayObject.shift()

        // arrayObject.shift()
        Array.prototype._shift = function () {
            let arr = this
            if (arr.length == 0) {
                return 
            }
            let result = arr[0]
            this.splice(0.1)
            return result
        }
        let arr1 = [1.2.3]
        console.log(arr1._shift());
Copy the code

5. arrayObject.unshift()

// arrayObject.unshift()
        Array.prototype._unshift = function () {
            let arr = this
            let beforeArr = []
            Array.prototype.forEach.call(arguments.item= > {
                beforeArr[beforeArr.length] = item
            })
            arr = [...beforeArr,...arr]
            console.log(arr);
            arr.forEach((item,index) = > {
                this[index] = arr[index]
            })
            console.log(this);
            return this.length
        }
        let arr1 = [1.2.3.4.5.6.7]
        console.log(arr1._unshift(6.8.9.44));
Copy the code

6. arrayObject.push()

// arrayObject.push() 
        Array.prototype._push = function () {
            Array.prototype.forEach.call(arguments.item= > {
                this[this.length] = item
            })
            console.log(this);
            return this.length
        }
        let arr1 = [1.2.3.4.5]
        console.log(arr1._push(55.44.66));
Copy the code

7. arrayObject.reverse()

// arrayObject.reserve()
        Array.prototype._reserve = function () {
            for (let i=0; i<this.length/2; i++) {let temp = this[this.length-1-i]
                this[this.length-1-i] = this[i]
                this[i] = temp
            }
        }
        let arr1 = [1.2.3.4.5]
        arr1._reserve()
        console.log(arr1);
Copy the code

8. arrayObject.fill()

Array.prototype._fill = function (val, start=0, end) {
            let arr = this
            let len = arr && arr.length || 0
            end = end || len
            start = start < 0 ? 0 : start // Set the start value of the loop
            end = end>len ? len : end // Set the end of loop value
            for(; start<end; start++) { arr[start] = val }return arr
        }
        let arr1 = [1.2.3]
        console.log(arr1._fill(7.0.2));
        console.log(arr1);
Copy the code

Methods that don’t change the array

1. arrayObject.slice()

Array.prototype._slice = function (start, end) {
            let arr = this
            start = start < 0 ? arr.length+start :start
            end = end && (end < 0 ? arr.length+end : end) || arr.length
            let result = []
            for(; start<end; start++) { result.push(arr[start]) }return result
        }
        let arr1 = [1.2.3.4]
        console.log(arr1._slice(0.1));
Copy the code

2. arrayObject.join()

Array.prototype._join = function (str) {
            let arr = this
            str = str ? str : ""
            let result = ""
            for (let i=0; i<arr.length-1; i++) { result = result+arr[i]+str } result = result+arr[arr.length-1]
            return String(result)
        }
        let arr1 = [1.2.3.4]
        console.log(arr1._join('$'));
Copy the code

3. arrayObject.toLocalString()

Array.prototype._tolocalString = function () {
            let arr = this
            return  arr.join(', ')}let arr1 = [1.2.3.4]
        console.log(arr1._tolocalString());
Copy the code

4. arrayObject.toString()

5. arrayObject.conat()

Array.prototype._concat = function () {
            let arr = this
            let result = arr
            Array.prototype.forEach.call(arguments.item= > {
                if (item instanceof Array) {
                    result = [...result, ...item]
                } else {
                    result = [...result, item]
                }
                
            })
            return result
        }
        let arr1 = [1.12.55.6.8]
        console.log(arr1._concat(1[5.5.6[7]],8));
        console.log(arr1 instanceof Array);
Copy the code

6. arrayObject.indexOf()

Array.prototype._indexOf = function (str, start=0) {
            let arr = this
            let result = -1
            for(; start<arr.length; start++) {if(str === arr[start]) {
                    result = start
                }
            }
            return result
        }
        let arr1 = [1.2.3.4.5]
        console.log(arr1._indexOf(1));
Copy the code

7. arrayObject.lastIndexOf()

Array.prototype._lastIndexOf = function (str, lastIndex) {
            let arr = this
            let result = -1
            lastIndex = lastIndex || arr.length-1
            while (lastIndex < 0) {
                lastIndex =  lastIndex+arr.length
            }
            if (lastIndex>arr.length) {
                return result  
            }
            for (; lastIndex >= 0; lastIndex--) {
                if (str === arr[lastIndex]) {
                    result = lastIndex
                }
            }
            return result

        }
        let arr1 = [1.2.3.4.5.6]
        console.log(arr1._lastIndexOf(1.3));
Copy the code

8. arrayObject.includes()

The includes method is designed to compensate for the shortcomings of the indexOf method:

1. The indexOf method does not recognize NaN

2. The indexOf method checks whether it contains a value that is not semantic enough. It needs to judge whether it is not equal to -1 and the expression is not intuitive enough

Array.prototype._includes =  function (str, start=0) {
            let arr = this
            if(start>=arr.length||! arr.length) {return false
            }
            for(; start<arr.length; start++) {if (str === arr[start]) {
                    return true}}return false

        }
        let arr1 = [1.2.3.4.'a']
        console.log(arr1._includes(4.0));
Copy the code

Traversal methods

1. arrayObject.forEach()

Array.prototype._forEach = function (func, thisArr) {
            let arr = thisArr || this
            if (typeoffunc ! = ='function') {
                throw new Error (func + 'is not a function')}for (let i=0; i < arr.length; i++) {
                func.call(arr,arr[i],i,arr)
            }
        }

        let arr1 = [1.2.3.4.5]
        console.log(arr1._forEach(item= >console.log(item)));
Copy the code

2. arrayObject.every()

Check whether all elements of the array meet the criteria

The every() method checks whether all the elements of the array meet the specified criteria (provided by the function).

The every() method uses the specified function to detect all elements in the array:

If one element in the array is detected as unsatisfactory, the entire expression returns false and the remaining elements are not tested.

Returns true if all elements meet the criteria.

Note: Every () returns true for an empty array.

Note: Every () does not change the original array.

Array.prototype._every = function (func, thisValue) {
            let arr = thisValue || this
            if (typeoffunc ! = ='function') {
                throw new TypeError(func + 'is not a function')}if (arr.length == 0) {
                return true
            }
            for (let i=0; i<arr.length; i++) {
               if(! func.call(arr,arr[i],i,arr) ) {
                   return false}}return true
        }
        function checkAdult(item) {
            return item >= 11
        }
 
        let arr = [32.33.16.40]
        let result = arr._every(checkAdult)
        console.log(result) // true
Copy the code

3. arrayObject.some()

Array.prototype._some = function (func, thisValue) {
            let arr = thisValue || this
            if (typeoffunc ! = ='function') {
                throw new TypeError (func + 'is not a function')}if (arr.length == 0) {
                return true
            }
            for (let i=0; i<arr.length; i++) {
                if (func.call(arr, arr[i], i, arr)) {
                    return true}}return false
        }
        let arr1 = [1.2.3.88]
        console.log(arr1._some(item= > item>100));
Copy the code

4. arrayObject.filter

Array.prototype._filter = function (func, thisValue) {
            let arr = thisValue || this
            let result = []
            if (typeoffunc ! = ='function') {
                throw new TypeError (func + 'is not a function')}if (arr.length == 0) {
                return result
            }
            for (let i=0; i<arr.length; i++) {
                if(func.call(arr, arr[i], i, arr)) {
                    result.push(arr[i])
                }
                
            }
            return result
        }
        let arr1 = [1.2.3.4.5.6]
        console.log(arr1._filter(item= >item>1));
Copy the code

5. arrayObject._map()

Array.prototype._map = function (func, thisValue) {
            let arr = thisValue || this
            let result = []
            if (typeoffunc ! = ='function') {
                throw new TypeError (func + 'is not a function')}if(! arr.length) {return result
            }
            for (let i=0; i<arr.length; i++) {
                arr[i] = func.call(arr, arr[i], i, arr)
            }
            return arr

        }
        let arr1 = [1.2.3.4.5.6]
        console.log(arr1._map(item= >item*10));
Copy the code

5. arrayObject.reduce()

Array.prototype._reduce = function (func, initValue=0) {
            let arr= this
            let result = initValue
            if (typeoffunc ! = ='function') {
                throw new TypeError (func + 'is not a function')}if(! arr.length) {return[]}for (let i=0; i<arr.length; i++) {
                result = func.call(arr, result, arr[i], i, arr)
            }
            return result
        }
        let arr1 = [1.2.30]
        console.log(arr1._reduce((total,item,i,arr) = > item+total));
Copy the code

6. arrayObject.find()

 Array.prototype._find = function (func, thisValue) {
            let arr = thisValue || this
            if (typeoffunc ! ='function') {
                throw new TypeError (func + 'is not a function')}if(! arr.length) {return undefined
            }
            for (let i=0; i< arr.length; i++) {
                if (func.call(arr, arr[i], i, arr)) {
                    return arr[i]
                }
            }
            return undefined

        }
        let arr1 = [1.2.3.44.55]
        console.log(arr1._find(item= > (item>10)));
Copy the code

7. arrayObject.findIndex()

Array.prototype._find = function (func, thisValue) {
            let arr = thisValue || this
            if (typeoffunc ! ='function') {
                throw new TypeError (func + 'is not a function')}if(! arr.length) {return -1
            }
            for (let i=0; i< arr.length; i++) {
                if (func.call(arr, arr[i], i, arr)) {
                    return i
                }
            }
            return -1

        }
        let arr1 = [1.2.3.44.55]
        console.log(arr1._find(item= > (item>10)));
Copy the code