Some commonly used methods of arrays

Five iteration methods for arrays :

forEach(): no return value, just calls the specified function (callbackfn) for each array item

every(): Returns a boolean value (true or false) to determine whether each array item meets the conditions of the specified function, if it is true, otherwise it is false

some(): Returns a boolean value (true or false) to determine whether each array item meets the conditions of the specified function, as long as any item returns true, it will return true

filter(): calls the specified function for each array item, and returns a new array if the condition is true

var arr = [1,2,3]
var arr_new= that.tag.filter(item => {
 return item != 1;
});
arr_new //[2,3]

 map(): call the specified function for each array item, and return the result of each function call to form a new array

var arr = [1,2,3]
var arr_new= arr.map(item => {
        return item+1;
});
arr_new //[2,3,4]

 Among the five array iteration methods, the forEach(), every() and some() methods do not generate a new array, while the filter() and map() methods will generate a new array (qualified).

 Array merge:

The concat() method is used to concatenate two or more arrays.

 This method does not change the existing array, but just returns a copy of the concatenated array.

var arr1 = [1,2];
var arr2 = [3.4];
var arr3 = [5.6];
var arr_new1= arr1.concat(arr2);
var arr_new2= arr1.concat(arr2,arr3);
arr_new1//[1,2,3,4]
arr_new2//[1,2,3,4,5,6]

 create string from array

The join() method is used to put all elements in an array into a string.

Elements are separated by the specified delimiter.

 

var arr = [110,112,119]
console.log(arr.join())//"110,112,119"
comma separated by default
console.log(arr.join('+'))//"110+112+119"
Delimiters can also be customized
 Array deletion and clearing delete method in this way, the length of the array remains unchanged
var arr = [22,33,44,55];
delete arr[1];
In this way, the length of the array does not change, and arr[1] becomes undefined.
 In the splice method  , the length of the array changes accordingly, but the original array index also changes accordingly. The first 1 in the splice parameter is the starting index of deletion (counting from 0), and the second element is the number of deleted elements  
var and = [1,2,3,4];
ary.splice(0,ary.length);//Empty the array
console.log(ary); // output [], empty array, that is emptied


var and = [11,22,33,44];
ary.splice(0,1);//Delete 11 in the array
Ary.splice ($. InArray (11, ary), 1);
Where $.inArray(2, ary) is used to find the index position of an element in the array.
While the splice method deletes array elements, it can also add new array elements , such as arr.splice(1,1,55,66), 55,66 two elements are added to the array arr

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326988410&siteId=291194637