Common operations on arrays

1.sort()

Description: According to the sorting rules provided, the array elements can be sorted. The sorting
           order can be letters or numbers, and in ascending or descending order. The default sorting order is alphabetically ascending.
           To use numerical sorting, you must pass a function as an argument to the call.
Syntax: arr.sort (sorting rules);
Note:
         (1) If no parameter is specified, the default is to sort according to the encoding method (Unicode size)
         (2) This method will change the structure of the original array

Compatibility: All major browsers support

var arr = [243,123,567,453,54,34,54,13,5,98,100];
var newArr = arr.sort(); 
console.log(arr); //(11) [100, 123, 13, 243, 34, 453, 5, 54, 54, 567, 98]
console.log(newArr); //(11) [100, 123, 13, 243, 34, 453, 5, 54, 54, 567, 98]
		
function sortFunc(a,b){
	return a-b;
};
var arr = [243,123,567,453,54,34,54,13,5,98,100];
var newArr = arr.sort(sortFunc);
console.log(arr); //(11) [5, 13, 34, 54, 54, 98, 100, 123, 243, 453, 567]
console.log(newArr); //(11) [5, 13, 34, 54, 54, 98, 100, 123, 243, 453, 567]

2.reverse()

Description: The order of the array elements can be reversed (reverse order)
Syntax: arr.reverse();
Note: This method can change the structure of the original array
Compatibility: All major browsers support

var arr = [11,34,54,67,53,23,1,32];
var newArr = arr.reverse();
console.log(arr); //(8) [32, 1, 23, 53, 67, 54, 34, 11]
console.log(newArr); //(8) [32, 1, 23, 53, 67, 54, 34, 11]

3.slice()

Description: Start intercepting from the array with the subscript fromIndex, intercept until the subscript toIndex, and return the intercepted array elements to form a new array Syntax:
arr.splice(fromIndex,toIndex);
Note:
         (1) When intercepting, it is Starting from fromIndex and ending at toIndex, the element at the subscript of toIndex is not included
         (2) The slice() method will not modify the original array structure

(3) The parameters of the slice method are allowed to be negative numbers, which means the compatibility of          the penultimate element : All major browsers support

var arr = ['aa','bb','cc','dd','ee','ff'];
var newArr = arr.slice(3,6);
console.log(arr); //(6) ["aa", "bb", "cc", "dd", "ee", "ff"]
console.log(newArr); //(3) ["dd", "ee", "ff"]

4.splice()

Description: Intercept and replace elements
Syntax: arr.splice(index, removeCount, addItem1, addItem2,...);
Note:
         (1) This method can change the original array structure
         (2) For this method, the first two parameters are necessary parameter. All the following parameters are optional
         . The number of elements intercepted by parameter (3) does not have to be consistent with the number of added elements.

Compatibility: All mainstream browsers support

var arr = ['123','aaa','bbb',4,6,5,'ddd','ccc','eee'];
var subArr = arr.splice(3,4,'a','b','c','1','2','3');
console.log(subArr); //(4) [4, 6, 5, "ddd"]
console.log(arr); //(11) ["123", "aaa", "bbb", "a", "b", "c", "1", "2", "3", "ccc", "eee"]

5.indexOf()

Description: Find the subscript of the first occurrence of the element in the array, and return -1
if          not
found
Subscript to start searching backwards
         (2) The second parameter is allowed to be a negative number, which means searching backwards from the penultimate element
         (3) [Whether it is a positive parameter or a negative parameter, the search order is always backwards 】

Compatibility: IE8 and its later versions do not support, the rest are supported

var arr = ['小红','小橙','小黄','小绿','小青','小红'];
var index = arr.indexOf('小红',2);
console.log(index); //5

6.lastIndexOf()

Description: Find the last index of the element in the array, if not found, return -1
Syntax: arr.lastIndexOf(item,toIndex);
Note:
         (1) The second parameter is an optional parameter, which represents the backward search The end of the subscript number
         (2) The second parameter is allowed to be a negative number, and the negative number means to search backward to the end of the [last few]

Compatibility: IE8 and later versions do not support it, and the rest support it

var arr = ['小红','小橙','小黄','小绿','小青','小红'];
var index = arr.lastIndexOf('小红',2);
console.log(index); //0

7.unshift()

Description: The function is to add one or more elements at the current starting position of the array, and return the new length of the added array
Syntax: arr.unshift(item1,item2,...);
Note: This method will affect the original array , you can decide whether to save the result according to your needs.
Compatibility: IE8 and earlier versions do not support returning undefined, and all others support it

var arr = ['小红','小橙','小黄'];
var newLength = arr.unshift('小绿','小青');
console.log(arr); //(5) ["小绿", "小青", "小红", "小橙", "小黄"]
console.log(arr.length); //5
console.log(newLength); //5

8.shift()

Description: The function is to delete the first element at the beginning of the array and return this element
Syntax: arr.shift();
Note: This method will affect the original array, and you can decide whether to save the result according to your needs.
Compatibility: All mainstream browsers devices support

var arr = ['一','二','三','四','五'];
var deleteItem = arr.shift();
console.log(arr); //(4) ["二", "三", "四", "五"]
console.log(arr.length); //4
console.log(deleteItem); //一

9.push()

Description: The function is to add one or more elements to the end of the array and return the new length of the array
Syntax: arr.push(item1,item2,...);
Note: You can decide whether to save the execution result of the push method according to your needs
           (1) The push() method will affect the original array
           (2) You can decide whether to save the execution result of the push() method according to your needs

Compatibility: All mainstream browsers support

var arr = [];
var newLength = arr.push('Licy',15);
console.log(arr); //(2) ["Licy", 15]
console.log(newLength); //2
	
var otherNewLength = arr.push('Lily');
console.log(arr); //(3) ["Licy", 15, "Lily"]
console.log(otherNewLength); //3

10.pop()

Description: The function is to delete the last element of the array and return this element
Syntax: arr.pop();
Note:
         (1) The pop method will affect the original array
         (2) You can decide whether to save pop() according to your needs The execution result of the method
         (3) The pop() method has no parameters

Compatibility: Chrome1.0, IE5.5, Firefox1.0, Safari, Opera support

var arr = ['aaa','bbb','ccc','ddd','eee'];
var deleteItem = arr.pop();
console.log(deleteItem); //eee
console.log(arr.length); //4
console.log(arr); //(4) ["aaa", "bbb", "ccc", "ddd"]

11.isArray()

Description: Used to determine whether a variable is an array. If it returns true, otherwise it returns false
Syntax: Array.isArray(variable name);
Note: This method is not called by an array variable, but must be called by Array
Compatibility: Chrome5, IE9.0, Firefox4, Safari5, Opera10.5 support

var arr = [11,22,33,44,55];
console.log(typeof arr); //object
console.log(Array.isArray(arr)); //true

12.valueOf()

Description: Returns the array itself.
           The primitive value is inherited by all objects derived from the Array object.
Syntax: arr.valueOf();
Note:
         (1) valueOf() will not change the original array
         (2) The valueOf() method is usually automatically called by JavaScript in the background and does not appear explicitly in the code.

Compatibility: All major browsers support

var arr = [2,53,23,12,6,23];
var newArr = arr.valueOf();
console.log(arr); //(6) [2, 53, 23, 12, 6, 23]
console.log(newArr); //(6) [2, 53, 23, 12, 6, 23]
console.log(arr === arr.valueOf()); //true

13.toString()

Description: The array elements in the array can be spliced ​​into a comma-separated string
Syntax: arr.toString();
Note: The toString() method can not only combine array elements, but also work for Boolean types
Compatibility: All All major browsers support

var arr = ['aaa','bbb','ccc','ddd'];
var str = arr.toString();
console.log(str); //aaa,bbb,ccc,ddd

var bool = true;
console.log(bool); //true
var str = bool.toString();
console.log(str); //true

14.join()

Description: The elements in the array can be combined into a string with a given separator as an interval.
Syntax: arr.join('delimiter');
Note: Whether it is cutting the string or merging the array, it will not be correct The original data has an impact
Compatibility: All major browsers support

//切割字符串
var str = 'hello world goodbye world';
var wordArr = str.split(' ');
console.log(wordArr); //(4) ["hello", "world", "goodbye", "world"]
	    
//合并数组
var newStr = wordArr.join(' ');
console.log(newStr); //hello world goodbye world
console.log(wordArr); //(4) ["hello", "world", "goodbye", "world"]

15.concat()

Description: This method is used to concatenate two or more arrays
Syntax: arr.concat(arr1,arr2,arr3,...);
Note: This method will not change the existing arrays, but will only return one of the concatenated arrays Copy
Compatibility: All major browsers support

var arr = [11,22,33];
var arr1 = ['大红','小红'];
var arr2 = ['aaa','bbb','ccc'];
console.log(arr.concat(arr1,arr2)); (8) [11, 22, 33, "大红", "小红", "aaa", "bbb", "ccc"]

 

Guess you like

Origin blog.csdn.net/weixin_55992854/article/details/117359022