SoFunction
Updated on 2025-04-13

Javascript Introduction to Learning Chapter 4: JS Objects and Arrays


7. Add the memory usage of arrays:
a[10] = “test”; //Add new elements
Memory usage:
for example:
a[0] = “1” ;
a[10] =” 10” ;
Then the js value allocates memory to elements with subscripts 0 and 10, and the 9 elements in the middle are not allocated;

Note: Arrays can also be added to objects;
for example;
var a = new Circle(1,2,3);
a[0]= “test” ;
This example defines a new object property named "0".
Adding an array element to an object does not make it an array.

8. Delete the array:
var a = [1,2];
 delete a[0];
alert(a[0])  //Output undefined
alert(a[1])  //Output 2
As can be seen from the example, delete delete is not actually deleted, but the element is set to undefined;

If you want to actually delete it, you can use(), etc.
for example:
var a = [1,2];
 delete a[0];
alert(a[0])  //Output undefined
alert(a[1])  //Output 2
();  //Delete the first element of the array
alert(a[0])  //Output 2
 alert("length:"+);
alert(a[1])  //Output undefined ;   1 has been deleted, but in fact the length of the array is only 1;

9, the length of the array:
a[49] = “a”;
// Then the length of this array is 50  ;

The length attribute is often used to traverse array elements;
for example:
   var  a  = [“a” , “b “ ,”c” ];
for(var i = 0 ; i<  ; i++){
alert(a[i]);
}

This is assumed that the elements are continuous, if not the case:
It is necessary to detect whether each element is defined: For example:
for(var i = 0 ; i<  ; i++){
if(a[i]){ 
alert(a[i]); 
}
}

Multidimensional array: a[i][j] ;


10. Some methods of arrays:
1): join() method:
Convert all elements of an array into strings.
For example: var  a  = [1,2,3];
var s=   ();   // Output s==1,2,3
Of course, you can also specify a separator;
for example;
   s = (“,”);  
This method is the opposite of (). split() splits a string into several fragments to create an array;

2): reverse () Method:
Reverse an array.
     var  a   = new Array(1,2,3);
();
var s  =  ();  //s == “3,2,1”

3): sort() method:
Sort
♂: If no parameters are passed, then the array elements are sorted alphabetically.
var a = new Array(“ee”,”df”,”b”);
()
var s = (“, ”);  // s ==  “b, df, ee” 
♂: If the parameters are passed:
for example:
var a = [33,4,1111,222]
(); // Sorting:  1111 ,222 ,33,4
(function(x,y){
       return x-y;
});
var s  = (); //Output  4, 33,222,1111

//It can be seen from the example that if the sorting is x > y, then the first parameter is ranked after the second parameter.
For example; 1111 ,222  -?    1111-222>0 -?     222, 1111
Also note the alphabetical sorting: Because js is case sensitive, when sorting, the characters are unified into uppercase or lowercase and then sort them.

4): concat() method:
var  a  = [1,2,3];
 a= (4, [5,6],7);
a=(); //Output 1, 2, 3, 4, 5, 6, 7
 alert(a)
Notice:
If there is an array in the array, it cannot be expanded.
for example:
var a  = [1,2,3];
 a = (4,[5,[6,6]],7);
alert(a);  //I can't see this
 a = ("|");
alert(a);  //After splitting, pay attention to a comma

-------------------------------------

 var  c  = [1,2,3];
 var  d  =new Array(1,2,3);
alert(c); //1,2,3
alert(d); //1,2,3
//The reason why Object is not output is because
//Array is an object with an additional functional layer.
//We remember his particularity.

5): slice() method:
Returns a fragment of the array. Similar to the substring method of a string.

6): splice() method:
First of all, he has only one letter difference between his method and slice, but his use is completely different.
He can be used to delete.
var a  = [1,2,3];
 a = (0,2);
alkert(a);  // Output 1, 2
 a = (1,2);
alert(a);  //  Output 2  . If it is a = (0 , 1);  Output  1
 a = (1,2);
alkert(a);  //  No array was deleted, output empty array

He can also insert arrays. Specific methods:
    var array1 = new Array("1","2","3","4");
(1,0,"5");//After the second element, insert 5; if the second parameter is 0, it will not be deleted.
(array1+"<br>");//Output  1, 5,2,3,4
(2,3,"7","8")  // Delete the 3 elements after the third element. That is, the third, fourth, and fifth elements. Then insert 7,8 in this position
(array1);//Output  1, 5,7,8
Note: Unlike concat(), splice does not expand the parameters it inserts. That is, if an array is inserted, it is inserted into the array itself, not the element of the array.
If concat() inserts an array, it will expand the array and insert elements in the array, but when it is inserted into the array
When there is an array, it will not expand.


7): push() method and pop() method:
push(): Append one or more arrays to the end of the array.
pop(): Delete the last element of the array.
var array1 = new Array("1","2","3","4");
    ("5");
(array1+"<br>"); //Output  1, 2,3,4,5
    ()  
(array1);//Output  1, 2,3,4

8): unshift() method and shift () method:
Sing the opposite tune with push and pop. . .
unshift(): Append one or more arrays to the head of the array.
shift (): Delete the first element of the array.


There are many methods for arrays, which also seem to be annoying. So everyone should be a little patient.
You can also refer to this article;
/?id=438



Summary: This chapter mainly talks about some methods of objects and arrays. It's hard to remember and understand. But everything is a process from difficult to easy. I didn't understand it once, read it again. . . Read the book a hundred times, and the meaning will appear. . . . . .
Maybe you don't need to read it a hundred times.   ^_^.


Do you feel that after reading so many JS concepts, you can’t wait to write some examples or something? All right.
Next chapter, let’s take some practical things. . . .
If you still don't understand, you can search for information on Google.