Javascript Array and Class Array Related Principle Detailed Explanation

  • 2021-09-11 19:16:07
  • OfStack

There are two ways to create arrays

1. var arr = []

2. var arr = new Array()

If there is only 1 parameter, the length of the array will be specified, and if there is only 1 parameter, it can only be shaped, as shown in the following example

var arr = new Array(10)
var arr = new Array (10.2)//will report an error
var arr = new Array(1, 2, 3) //arr = [1, 2, 3]

Common methods of arrays

Change the original array push pop shift unshift sort reverse splice

Do not change the original array concat join-- > split toString slice

For example, push function, look at 1 example


var arr = [1, 2, 3]
arr.push(4, 5) //arr = [1, 2, 3, 4, 5]  To the end of the array 1 Bit, and the parameters can be multiple 
// Implementation principle 
Array.prototype.push = function() {
    for(var i = 0; i < arguments.length; i++) {
          this[this.length] = arguments[i]
    }
}

The function function is as follows


arr.pop()  //arr = [1, 2, 3, 4]  Delete the last of the array 1 Bit 
arr.unshift(-1, 0) //arr = [-1, 0, 1, 2, 3, 4]  To the front of the array 1 Bit to add elements,   Parameters can also be multiple 
arr.shift() //arr = [0, 1, 2, 3, 4]  Delete the front of the array 1 Bit 
arr.reverse() //arr = [4, 3, 2, 1, 0]  Array inverse, reverse order 
arr.splice(0, 3, 2, 2) //arr = [2, 2, 1, 0]  Parameter 1( Can be negative ,  From the last place ) From the first parameter 1 Bit, parameter 2 Delete the parameter 2 Bit,   Parameter 3 In the back,   Add parameters from deletion 3 And the following parameter data 
arr.sort(function(a, b) { // Sort   Parameter time 1 Functions, function parameters 1 Before Array 1 Number, parameter 2 After an array 1 A,   Return   Less than 0 Count in front and put it in front   Returns a value greater than 0 Count in front and put it behind   Return 0 Don't move 
    return a > b // Ascending order 
  // return a < b // Descending order 
})

Look at the following example to realize an array out of order


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

Look at the example of 1, which is to find the number of bytes of 1 string string


function retBytes(str) {
    var num = str.length
    for(var i = 0; i < str.length; i++) {
          if(str.charCodeAt(i) > 255) num++
    }
    return num
}

Look at the following example again


var arr = [1, 2, 3]
console.log(arr.concat(4, 5))
console.log(arr.concat([4, 5])) //1 Sample   Splice two arrays to return a new array without changing the original array 

console.log(arr.toString()) //1, 2, 3  Stringization 

console.log(arr.slice(0, 2)) //[1, 2]  Parameter 1 From the parameter 1 Interception,   Parameter 2 , truncated to the parameter 2 Bit 
console.log(arr.slice(1))  // From the first 1 Bit begins to truncate to the end 1 Bit 

console.log(arr.slice()) // No change,   But is used to set the 1 Class array is truncated as 1 An array of,   For example arguments

console.log(arr.join(',')) //1,2,3  With parameters 1 Connect to form a string 

var str = '1,2,3'
console.log(str.split(',')) //[1,2,3]  With parameters 1 Split into arrays, and join Reciprocal inverse 

var arr = [str, str1, str2, str3] // Multiple str Connect   Hash 
arr.join('')


Class array

E.g. arguments

Look at an example of a class array


var obj = {
    '0': 'a',
    '1': 'b',
    '2': 'c',
    'length': 3,
    'push': Array.prototype.push,
    'splice': Array.prototype.splice
}
// To be an index (numeric) property, you must have a length Property, preferably with push Method 

Look at an example of class array operation


var obj = {
    '2': 'a',
    '3': 'b',
    'length': 2,
    'push': Array.prototype.push
}
obj.push('c')   //obj[obj.length] = 'c' ->  At this time   Attribute '2' Is overwritten with  'c', length Become 3
obj.push('d')       //obj[obj.length] = 'd' ->  At this time   Attribute '3' Is overwritten with  'd', length Become 4
console.log(obj) //{2: 'c', 3: 'd', length: 4, push: Array.prototype.push}

Class arrays can also add other attributes, such as the following


var obj = {
    '0': 'a',
    '1': 'b',
    '2': 'c',
    'length': 3,
    'push': Array.prototype.push,
    'splice': Array.prototype.splice,
    'name': 'lyj',
    'age': 18
}

Related articles: