Summary of 15 Arrays Used in JavaScript

Posted by stageguys on Sat, 07 Sep 2019 09:22:03 +0200

1.push adds one or more items to the end of an array
The parameters can be one or more.
The return value is the length of the new array
Modify the original array

var ary = [1,2];
var num = ary.push('a');
console.log(num); //3
console.log(ary); //[1, 2, "a"]

2.pop deletes the last item of the array
No parameters
Return value is deleted item
Modify the original array

var ary = ["a", "b",1,2];
var a = ary.pop();
console.log(ary);//["a", "b", 1]
console.log(a); //2

3.unshift adds one or more items to the front of the array
Parameters can be one or more
The return value is the length of the new array
Modify the original array

var ary = ["a", "b",1,2];
var num = ary.unshift('3','4');
console.log(num); //6
console.log(ary); //["3", "4", "a", "b", 1, 2]

4.shift deletes the first item of the array
No parameters required
The return value is the deleted item
Modify the original array

 var ary = ["a", "b","c","d"];
 var ary1 = ary.shift();
console.log(ary1); //a
console.log(ary); //["b", "c", "d"]

5.splice can add one or more items to a location in an array, and can also delete and replace items
The return value is an array of deleted items
Modify the original array
Parameters:
One parameter, ary.splice(0), deleted from 0 to the end
Two parameters, ary.splice(1, 2), 1 is the index, 2 is the number of deletions
Multiple parameters, ary.splice(1, 2, "x", "y"... 1 is the index, 2 is the number of deletions, "x", "y",... Add to the original deleted location;

6.indexOf returns the location of the first occurrence of a specified character in a string or array. There is no subscript-1 to return, and there is an index value to return the first match.
Parameter: The item being queried;
Return value: first matched index; no return subscript - 1 exists;
Do not modify the original array;

7.lastIndexOf returns the location of the last occurrence of a specified character in a string or array. There is no subscript-1 returned, and there is an index value returned for the first match.
The parameter is the item being queried
Return value: index value, no return subscript - 1
Do not modify the original array

var array = ['abc','def','xyz','def'];
var f = array.indexOf('def');
var l = array.lastIndexOf('def');
console.log(f); //1
console.log(l); //3
console.log(array); //["abc", "def", "xyz", "def"]

8.slice interception
An array whose return value is intercepted
Do not modify the original array
Cloning of arrays without parameters
When the parameter is negative, the default is to add the length of the array to the positive number.

var arr = [1,2,3,4,5,6,7];
var newarr = arr.slice(1,5);  //Before and after the package
var newarr1 = arr.slice(-3,-1);
console.log("arr" + arr);   //1,2,3,4,5,6,7
console.log("newarr" + newarr); //2,3,4,5
console.log("newarr1" + newarr1);

9.sort sort modifies the original array
An array whose return value is sorted
Only numbers with the same number of digits can be sorted if there are no parameters

var arr = [1,100,33,77,500,66];
arr.sort();
console.log(arr);//[1, 100, 33, 500, 66, 77]
      
arr.sort(function(a,b){
	//From childhood to platoon
	return a-b;
	//From Big to Small Row
	return b-a;
 });
console.log(arr);

10.reverse inversion does not require parameter passing
Modify the original array

var arr = [1,100,33,77,500,66];
arr.reverse();
console.log(arr);

11.concat does not modify the original array, the return value is a spliced new array

var arr = [1,2];
var arr1 = arr.concat([3,4],[5,6],[7,8]);
console.log(arr1);

Cloning Representing Array without Passing Parameters at Conat

var arr2 = [1,3,5];
var arr3 = arr2.concat();
console.log(arr2 == arr3);  //false
console.log(arr2);//[1, 3, 5]
console.log(arr3); //[1, 3, 5]
//This is also the way to copy arrays.
var arr4 = [100,200,300];
var arr5 = arr4;
console.log(arr4 == arr5); //true
console.log(arr5);

12.join splices arrays into strings.
Do not modify the original array

var ary = ['2019','06','10']
var ary1 = ary.join("-")
console.log(ary1);//2019-06-10

By default, commas are used to separate parameters when they are not passed

var ary2 = ary.join();
console.log(ary2);//2019,06,10

var ary3 = ary.join("");
console.log(ary3);//20190610

13.map maps a new array based on each item of the original array
The parameter is a callback function
Return value is a new array mapped without modifying the original array
The callback function executes several times as long as the array is

var ary = ["a","b","c","d","e"];
var ary1 = ary.map(function(v,i,arr){ 
	//v value traverses every item of an array
	console.log(v);
	//i index traversal array index
	console.log(i);
	//arr primitive array
	console.log(arr);

	return v+'o'; //["ao", "bo", "co", "do", "eo"]
	//The return value is each item corresponding to the new array
});
console.log(ary1);

14.forEach is used to traverse arrays
The parameter is a callback function
Feature: No return value, no modification of the original array
The callback function executes several times as long as the array is

var arry = [11,22,33,44];
var arry1 = arry.forEach(function(v,i,arr){
	 //v value traverses every item of an array
	console.log(v);
	//i index traversal array index
	console.log(i);
	//arr primitive array
	console.log(arr);

	 return v+'o'; //no return value
 });
console.log(arry1); //undefined

forEach is equivalent to for loop

for (var i = 0; i < arry.length; i++) {
	arry[i];
	i;
	arry;
}

15.toString converts an array into a string without modifying the original array
No parameters are required. The return value is a string.

var array = ["q","w","e","r"];
var array1 = array.toString();
console.log(array1);//q,w,e,r

Attention:
Class arrays cannot use array methods
Class array refers to the class array obtained by arguments, document. getElements ByTagName