Common methods of js array

1. join()

  • join is to convert an array into a string, and then specify a connecting character for it. The default is comma (,)

  • Writing format: join(""), write a string in parentheses ("quotation marks required"),

var arr = [1,2,3];
console.log(arr.join());  // 1,2,3
console.log(arr.join("-")); // 1-2-3
console.log(arr);   
// [1, 2, 3 (original array unchanged)

2. push() and pop()

  • push(): add the contents to the end of the array and return the modified length.

  • pop(): remove the last item of the array, return the removed value, and reduce the length of the array.

  • Writing format: arr.push(""), write content in parentheses ("string should be quoted"),

  • Writing format: arr.pop()

var arr = ["Lily","lucy","Tom"];
var count = arr.push("Jack","Sean");
console.log(count);   // 5
console.log(arr);   
// ["Lily", "lucy", "Tom", "Jack", "Sean"]
var item = arr.pop();
console.log(item);   // Sean
console.log(arr);   
// ["Lily", "lucy", "Tom", "Jack"]

3. shift() and unshift()

  • shift(): deletes the first item of the original array and returns the value of the deleted element; If the array is empty, undefined is returned.

  • unshift: adds a parameter to the beginning of the original array and returns the length of the array.

  • Writing format: arr.shift(""), write content in parentheses ("string should be quoted"),

var arr = ["Lily","lucy","Tom"];
var count = arr.unshift("Jack","Sean");
console.log(count);               // 5
console.log(arr);                //["Jack", "Sean", "Lily", "lucy", "Tom"]
var item = arr.shift();
console.log(item);   // Jack
console.log(arr);   
// ["Sean", "Lily","lucy", "Tom"]

4. sort()

  • sort(): sort the items in the array from small to large

  • Writing format: arr.sort()

var arr1 = ["a", "d", "c", "b"];
console.log(arr1.sort());  
// ["a", "b", "c", "d"]
  • The sort() method compares strings and does not sort numbers according to the size of numbers. To achieve this, you must use a sort function
function sortNumber(a,b)
{
  return a - b
}
// give an example:
arr = [13, 24, 51, 3]; console.log(arr.sort());           
// [13, 24, 3, 51] 
console.log(arr.sort(sortNumber));     
// [3, 13, 24, 51] (array changed)

5. reverse()

  • reverse(): reverses the order of array items.

  • Writing format: arr.reverse()

var arr = [13, 24, 51, 3];
console.log(arr.reverse());        
 //[3, 51, 24, 13]
console.log(arr);              
 //[3, 51, 24, 13] (original array changed)

6. concat ()

  • concat(): add parameters to the original array. This method will first create a copy of the current array, then add the received parameters to the end of the copy, and finally return the newly constructed array. Without passing arguments to the concat () method, it simply copies the current array and returns a copy.
  • Writing format: arr.concat(), write the content in parentheses ("the string should be quoted"),
var arr = [1,3,5,7];
var arrCopy = arr.concat(9,[11,13]);
console.log(arrCopy);             
//[1, 3, 5, 7, 9, 11, 13]
console.log(arr);               
// [1, 3, 5, 7] (original array not modified)

7. slice()

  • slice(): returns a new array consisting of items from the specified start subscript to the end subscript in the original array. The slice () method can accept one or two parameters, that is, the start and end positions of the item to be returned. In the case of only one parameter, the slice () method returns all items from the position specified by the parameter to the end of the current array. If there are two arguments, the method returns the item between the start and end positions -- but not the item at the end position.
  • Writing format: arr.slice (1, 3)
var arr = [1,3,5,7,9,11];
var arrCopy = arr.slice(1);
var arrCopy2 = arr.slice(1,4);
var arrCopy3 = arr.slice(1,-2);
var arrCopy4 = arr.slice(-4,-1);
console.log(arr);               
//[1, 3, 5, 7, 9, 11] (the original array remains unchanged)
console.log(arrCopy);             
//[3, 5, 7, 9, 11]
console.log(arrCopy2);            
//[3, 5, 7]
console.log(arrCopy3);            
//[3, 5, 7]
console.log(arrCopy4);            
//[5, 7, 9]
  • arrCopy only sets one parameter, that is, the starting subscript is 1, so the returned array starts from subscript 1 (including subscript 1) to the end of the array.

  • arrCopy2 sets two parameters to return a subarray of starting subscripts (including 1) and ending subscripts (excluding 4).

  • arrCopy3 sets two parameters. The termination subscript is a negative number. When a negative number occurs, the number at this position is replaced by the negative number plus the value of the array length (6), so it is a sub array from 1 to 4 (not included).

  • Both parameters in arrCopy4 are negative numbers, so they are converted into positive numbers by adding array length 6, so they are equivalent to slice(2,5).

8.splice()

  • splice(): delete, insert, and replace.

  • Delete: specify 2 parameters: the position of the first item to be deleted and the number of items to be deleted.

  • Writing format: arr.splice (1, 3)

  • Insert: you can insert any number of items into the specified location. You only need to provide three parameters: starting location, 0 (number of items to be deleted) and items to be inserted.

  • Writing format: arr.splice (2,0,4,6)

  • Replace: you can insert any number of items into the specified location and delete any number of items at the same time. You only need to specify three parameters: starting location, number of items to delete and any number of items to insert. The number of items inserted does not have to be equal to the number of items deleted.

  • Writing format: arr.splice (2,0,4,6)

var arr = [1,3,5,7,9,11];
var arrRemoved = arr.splice(0,2);
console.log(arr);                
//[5, 7, 9, 11]
console.log(arrRemoved);            
//[1, 3]
var arrRemoved2 = arr.splice(2,0,4,6);
console.log(arr);                
// [5, 7, 4, 6, 9, 11]
console.log(arrRemoved2);           // []
var arrRemoved3 = arr.splice(1,1,2,4);
console.log(arr);                
// [5, 2, 4, 4, 6, 9, 11]
console.log(arrRemoved3);           
//[7]

9.indexOf() and lastIndexOf()

  • indexOf(): receives two parameters: the item to find and (optional) the index indicating the starting position of the search. Where, look backward from the beginning of the array (position 0).
  • Writing format: arr.indexof (5)
  • lastIndexOf: receives two parameters: the item to find and (optionally) the index representing the starting position of the search. Where, look forward from the end of the array.
  • Writing format: arr.lastindexof (5,4)
var arr = [1,3,5,7,7,5,3,1];
console.log(arr.indexOf(5));       
//2
console.log(arr.lastIndexOf(5));    
//5
console.log(arr.indexOf(5,2));      
//2
console.log(arr.lastIndexOf(5,4));   
//2
console.log(arr.indexOf("5"));      
//-1

10. forEach()

  • forEach(): loop through the array and run the given function for each item in the array. This method has no return value. The parameters are all of function type. By default, there are passed parameters. The parameters are: the contents of the traversed array; The corresponding array index, the array itself.
  • Writing format: arr.forEach()
var arr = [1, 2, 3, 4, 5];
arr.forEach(function(x, index, a){
console.log(x + '|' + index + '|' + (a === arr));
});
// Output is:
// 1|0|true
// 2|1|true
// 3|2|true
// 4|3|true
// 5|4|true

11. map()

  • map(): refers to "mapping", which runs a given function on each item in the array and returns the result of each function call.
  • Writing format: arr.map()
var arr = [1, 2, 3, 4, 5];
var arr2 = arr.map(function(item){
return item*item;
});
console.log(arr2);        
//[1, 4, 9, 16, 25]

12.filter()

  • filter(): "filter" function. Each item in the array runs the given function and returns an array that meets the filtering conditions.
  • Writing format: arr.filter()
var arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
var arr2 = arr.filter(function(x, index) {
return index % 3 === 0 || x >= 8;
}); 
console.log(arr2);         
//[1, 4, 7, 8, 9, 10]

13. every()

  • every(): judge whether each item in the array meets the conditions. true will be returned only if all items meet the conditions.
  • Writing format: arr.every()
var arr = [1, 2, 3, 4, 5];
var arr2 = arr.every(function(x) {
return x < 10;
}); 
console.log(arr2);         //true
var arr3 = arr.every(function(x) {
return x < 3;
}); 
console.log(arr3);         // false

14. some()

  • some(): judge whether there are items that meet the conditions in the array. As long as one item meets the conditions, it will return true.
  • Writing format: arr.some()
var arr = [1, 2, 3, 4, 5];
var arr2 = arr.some(function(x) {
return x < 3;
}); 
console.log(arr2);         //true
var arr3 = arr.some(function(x) {
return x < 1;
}); 
console.log(arr3);         // false

Keywords: Javascript Front-end Interview

Added by Pha on Thu, 27 Jan 2022 09:21:04 +0200