javascript array

[ Declare an array] var a=[1,2,3];

[ Define the length of the array] var a=new Array(2);

[ Connect array] array 1.concat(array 2, array 3)

[ Convert the array to a string, you can customize the connector] arr.join("connector")

[ Convert an array to a string and return an array] arr.toString()

[ Return the last digit of the array and delete the value] arr.pop() [Display the last digit first, then delete it]

[ Return the last digit of the array and delete the value] arr.shift() [Display the first digit first, then delete it]

[ Add one digit to the end of the array and return the length number] arr.push (the first added, the second added)

[ Add one digit to the first position of the array and return the length number] arr.unshift (the first added, the second added)

[ Reverse the array] arr.reverse()

[ Intercept array (including start and end positions)] arr.slice (start position, end position)

[ Sort by the first letter of the text] arr.sort();
[ Sort by number size] arr.sort(sortNumber); [The following function needs to be added above]

 

[ Add or delete items from the array, return the deleted items ] splice(start, deletecount, item1, ..., itemX)
1. The shift() method removes the first element of the array from it and returns the value of the first element.
Returns undefined if the array is empty
var arr = [];
arr[0]=a;
arr[1]=b;
console.log(arr); // a arr.push(
'd','e','f'); console.log(arr); // (4) ["b", "d", "e", "f" ]

2. The unshift() method adds one or more elements to the beginning of the array

arr.unshift(1,2); //同 1 例子
console.log(arr);   //[1,2,"b","d","e","f"]
res=arr.shift();       //删除的第一个元素是1,返回1 console.log(res); // 1

三、pop()方法用于删除数组最后一个元素,并返回最后一个元素

var res=arr.pop(); //同 1 例子
console.log(res);   // f
console.log(arr)      //(4) [ "a", "b", "d","e"]
四、push() 在数组的尾部添加一个元素,并返回新数组的长度
var arr=new Array(3)
arr[0]="a"
arr[1]="b"
arr[2]="c"
console.log(arr) //(3) ["a", "b", "c"]
console.log(arr.push("d")) // 4
console.log(arr) //(4) ["a", "b", "c", "d"]
五、concat()用于连接两个或多个数组
var a=[1,2,3];
console.log(a.concat(4,5))   //[1,2,3,4,5]
六、join()用于把数组中的所有元素放入一个字符串,元素是通过指定的分隔符进行分隔的。
var arr2=new Array(3)
arr2[0]='abc'
arr2[1]='john'
arr2[2]='Toni'
console.log(arr2.join())   //abc,john,Toni    
七、sort()方法用于对数组的元素进行排序,按文字首字母排序
  【按数字大小排序】arr.sort(sortNumber);【需要在上面加以下函数】
var arr = new Array(6)
arr[0] = "George"
arr[1] = "John"
arr[2] = "Thomas"
arr[3] = "James"
console.log(arr)   //["george","John","Thomas","James"]
console.log(arr.sort())    //

var arr2=[1,3,2,6,4]
console.log(arr2.sort()) //[1,2,3,4,6]

八、reverse():将数组反序 , 方法用于颠倒数组中元素的顺序。

var arr4=new Array(3)
arr4[0]='abc'
arr4[1]='john'
arr4[2]='Toni'
console.log(arr4.reverse())  //['Toni','John','abc']
九、slice()返回从原数组中指定开始下标到结束下标之间的项组成的新数组,不含尾
var a=[1,2,3,4,5]
var b=a.slice(2,4);
console.log(b);      // 3,4    
十、splice(start,deletecount,item1,...,itemX)方法从数组中添加或删除项目,返回被删除的项目
deletecount要删除的项目数量。如果设置为 0,则不会删除项目。
var a = [1,2,3,4,5];   
var b = a.splice(2,2,7,8,9);  
console.log(a)        //[1,2,7,8,9,5]
console.log(b)        //[3,4]
删除位于index1的元素,并添加一个新元素来替代被删除的元素:
var arr6=new Array(3)
arr6[0] = "George"
arr6[1] = "John"
arr6[2] = "Thomas"
console.log(arr6)     // ["George", "John", "Thomas"]
arr6.splice(1,1,"123")
console.log(arr6)     //["George", "123", "Thomas"]

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324807169&siteId=291194637