Summary of common methods of arrays and objects

Common methods of arrays

1, shift() method: delete the first element of the array and return the value of the first element

var a = [ ' a ' , ' b ' , ' c ' ];
console.log(a,a.shift());
//['b','c']     'a'

2, unshift() : add the parameter to the beginning of the original array and return the length of the array 

var movePos = [ 111 , 222 , 333 , 444 ];
movePos.unshift("55555")
document.write(movePos + "<br />")     //55555,111,222,333,444

3, pop(): used to delete and return the last ( deleted element) element of the array, if the array is empty, return undefined, reduce the length of the array by 1

var a = [ ' a ' , ' b ' , ' c ' ];
console.log(a,a.pop());
//["a", "b"]      "c"

4, push(): Add one or more elements to the end of the array and return the new length (used to change the length of the array).

var movePos = [ 11 , 22 ];

var arr=movePos.push("333");

console.log(movePos,arr) //[11, 22, "333"] 3

5, concat() method: used to connect two or more arrays and return a new array, the new array is formed by adding parameters to the original array 

var movePos=[11,22];
var arr=movePos.concat(4,5);
console.log(arr);//[11, 22, 4, 5]

6, join () method: used to put all the elements in the array into a string. Elements are separated by the specified delimiter.

var movePos=[11,22];
var arr=movePos.join("+");
console.log(arr)  //11+22

7, slice () method: can return the selected element from the existing array. slice (start interception position, end interception position)

var movePos = [ 11 , 22 , 33 ];
var arr = movePos.slice ( 1 , 2 );
console.log(arr)//[22]

8, splice() method: The method adds/removes items to/from the array, and then returns the deleted item.

var movePos=[ 11 , 22 , 33 , 44 ];
 var arr=movePos.splice( 1 , 2 );//delete
console.log(arr,movePos)
 [22, 33]     [11, 44]

  var movePos =[111,222,333,444];
  movePos.splice(2,1,"666")
  console.log(movePos)

  [111, 222, "666", 444]

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

The split: method is used to split a string into an array of strings.

var host="?name=232&key=23";
host=host.split("?")
console.log(host)
["", "name=232&key=23"]

The substring() method is used to extract characters in a string between two specified subscripts.

var host="?name=232&key=23";
host=host.substring(1)
console.log(host)
//name=232&key=23

There is no time left

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Reference: https://www.cnblogs.com/js0618/p/6283724.html

Guess you like

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