SoFunction
Updated on 2025-03-01

Detailed explanation of the principles related to Javascript arrays and class arrays

There are two ways to create arrays

1. var arr = []

2. var arr = new Array()

If there is only one parameter, it will specify the length of the array, and when one parameter, it can only be a plastic shape, as shown in the following example

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

Common methods for arrays

Change the original array push pop shift unshift sort reverse splice

Don't change the original array concat join ---> split toString slice

For example, push function, see the example

var arr = [1, 2, 3]
(4, 5) //arr = [1, 2, 3, 4, 5] Add elements to the last bit of the array, with multiple parameters//Principle of implementation = function() {
for(var i = 0; i < ; i++) {
this[] = arguments[i]
}
}

The function function is as follows

()  //arr = [1, 2, 3, 4] Delete the last bit of the array(-1, 0) //arr = [-1, 0, 1, 2, 3, 4] Add elements to the front bit of the array, and multiple parameters can also be() //arr = [0, 1, 2, 3, 4] Delete the front bit of the array() //arr = [4, 3, 2, 1, 0] Array inverted, reverse order(0, 3, 2, 2) //arr = [2, 2, 1, 0] Parameter 1 (can be negative, starting from the last digit), starting from the first bit of parameter, parameter 2, delete parameter 2, and after parameter 3, add parameter 3 and subsequent parameter data from the delete(function(a, b) { //Sorting a function when the parameter is used. Function parameter 1 is the previous one in the array and parameter 2 is the next one in the array. Return less than 0, put the previous number in front Return greater than 0, put the previous number in front Return 0, don't movereturn a &gt; b //Ascending order  // return a < b // descending order})

See the following example to implement an array disordered order

var arr = [1, 2, 3, 4, 5, 6, 7]
(function(a, b) {
return () - 0.5
})
(arr)

Take a look at the example to find the number of bytes of a string

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

Look at the following examples

var arr = [1, 2, 3]
((4, 5))
(([4, 5])) //Same, splicing two arrays returns a new array without changing the original array
(()) //1, 2, 3 String
((0, 2)) //[1, 2] Parameter 1, intercepted from parameter 1, parameter 2, intercepted from parameter 2 bits((1))  //Seave the last one from the first position
(()) //Unchanged, but is used to intercept an array of classes into an array, such as arguments
((',')) //1,2,3 Connect the parameter 1 to form a string
var str = '1,2,3'
((',')) //[1,2,3] is divided into an array with parameter 1, and is inversely related to join
var arr = [str, str1, str2, str3] // Multiple strs are connected to hash('')


Class array

For example arguments

See an example of an array of classes

var obj = {
'0': 'a',
'1': 'b',
'2': 'c',
'length': 3,
'push': ,
'splice': 
}
//The attribute should be indexed(number)property,Must havelengthproperty,It's best to havepushmethod

See an example of a class array operation

var obj = {
'2': 'a',
'3': 'b',
'length': 2,
'push': 
}
('c')   //obj[] = 'c' -> At this time, the value of attribute '2' is overwritten as 'c', length becomes 3('d') //obj[] = 'd' -> At this time, the value of attribute '3' is overwritten as 'd', and length becomes 4(obj) //{2: 'c', 3: 'd', length: 4, push: }

Other properties can be added to the class array, as follows

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

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.