Syntax
var 数组名= new Array();
When a numeric value is passed in the brackets, it indicates the length of the array. When a value greater than 1 is passed, it indicates adding elements to the array.
Syntax 2:
var 数组名=[];
When a value is passed inside square brackets, an element is added to the array.
Object
is returned when using typeof
to check an array object.
1. Directly add
syntax:
数组名= [元素1,元素2...];
2. Add according to index (where to add the element)
syntax:
数组名[索引] = '元素';
It is worth noting here that if elements are added according to index, the length of the array is calculated according to the maximum index added.
Reading array elements is a return value, which requires a variable to receive or output directly.
1. Direct reading, that is, reading the elements in the entire array.
Syntax:
console.log(数组名);
2. Read an element in the array.
Syntax:
console.log(数组名[索引]);
3. When reading an element that does not exist, return undefined.
The length attribute can set/return the length of the array.
数组名.length = 长度;
数组名.length;
Additional tip: You can use the length property to add an element to the last position of the array.
The concat()
method is used to connect two or more arrays.
Syntax:
数组1.concat(数组2,数组3...);
The join()
method is used to convert all elements in the array into a string.
Syntax:
数组名.join(分隔符,不写则默认为逗号分隔);
The pop()
method is used to delete the last element of the array and return the deleted element .
Syntax:
数组名.pop();
The push()
method adds one or more elements to the end of the array and returns the new length .
Syntax:
数组名.push(元素1,元素2...);
The shift()
method is used to delete the first element of the array and return the value of the first element.
Syntax:
数组名.shift();
The unshift()
method adds one or more elements to the beginning of the array and returns the new length .
Syntax:
数组名.unshift(元素1,元素2...);
The slice()
method can return selected elements from an existing array. Extracts a portion of a string and returns the extracted portion as a new string. Note: The slice() method does not alter the original array.
Syntax:
数组名.slice(start,end);
the interval is left closed and right open. If it is a negative number, it is taken from the last number of the array.
The splice()
method is used to add or delete elements in an array.
Syntax:
数组名.splice(从哪个位置开始(必填),要删除的元素个数(可选),要添加的元素(可选))
The reverse()
method is used to reverse the order of elements in an array.
Syntax:
数组名.reverse();
The sort()
method is used to sort the elements of the array. The sort order can be alphabetical or numerical, and in ascending or descending order. The default sort order is ascending alphabetically.
Syntax:
数组名.sort();
1. Sort alphabetically (ascending/descending order)
2. Sorting by numbers (from large to small/small to large) requires defining a function.
uses a for loop to traverse an array.
Syntax:
for(var 变量=0; 变量<数组名.length;变量++){console.log(数组名[变量])}
adds 6 objects to the array and adds the adult list of people to a new array! !
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <script> function Person(name, age){ this.name = name; this.age = age; } var per1 =new Person('Su Liang',21); var per2 =new Person('小红',15); var per3 =new Person('小月',17); var per4 =new Person('Xiaoli',19); var per5 =new Person('Xiao Shui',20); var per6 =new Person('小花',5); var per_list= [per1,per2,per3,per4,per5,per6]; function arrAdult(){ var newArr=[]; for(var i = 0;i<per_list.length;i++){ var x = per_list[i]; if(x.age<18){ console.log(x.name + 'failed'); }else if(x.age>=18){ console.log(x.name + 'Congratulations, you passed!'); newArr.push(x.name) } } return newArr; } var list = arrAdult(); console.log('Passed list: '+ list) </script></head><body> </body></html>
Run results:
The above is the detailed summary of array knowledge points in JavaScript. For more information, please pay attention to other related articles on the PHP Chinese website!