Js Array
Contents
Method without changing the original array:
indexOf()andlastIndexOf()1
indexOf(): Returns the index of the element in the array, starting from0. If the element does not exist in the array,-1is returned.
var arr = [1, 2];
arr.indexOf(1); //0
arr.indexOf(10); //-1
- 2
lastIndexOf(): Returns the index of the last occurrence of an element in the array, or -1 if it does not appear.
var arr = [1, 2, 3, 4, 2];
arr.lastIndexOf(2); // 4
slice(): Like thesubstring()method ofstring, it intercepts a part of the array and returns a new array.1 Usually, it accepts 2 parameters as a left-closed and right-open interval, that is, it includes the element at the starting index position, but does not include the element at the ending index position.
var arr = [1, 2, 3,4,5,6];
arr.slice(0,2) // [1, 2] only returns elements at index 0,1
- 2 You can omit the second parameter, that is, intercept the last element of the original array.
arr.slice(2,); //[3, 4, 5, 6]
- 3 If no parameters are passed, a new array intercepting all elements from beginning to end will be returned. Can be used to copy an array.
var copyArr = arr.slice();
copyArr; //[1, 2, 3, 4, 5, 6]
concat(): merge arrays. Concatenates the current array with another array and returns a new array.1 The parameters of the
concat()method can have multiple parameters, or any type, such asnumeric,string,Boolean,array, andobject. The parameters will be added to the new array.
var arr1 = [1, 2, 3,4,5,6];
var arr2 = ['a','b','c'];
var arr3 = arr1.concat(arr2);
arr3; //[1, 2, 3, 4, 5, 6, "a", "b", "c"]
- 2 Note that if the parameter is an
array, it will be flattened once, that is, the array will be split and added to a new array. See specific examples:
var arr1 = [1, 2, 3];
var arr2 = arr1.concat(66,'abc',true,[10,20],[30,[31,32]],{x:100});
arr2; //[1, 2, 3, 66, "abc", true, 10, 20, 30, [31,32], {x:100}]
join(): Convert to string. It will concatenate each element of the current Array with the specified string, and then return the concatenated string.1 The parameter is a string used to specify the connection. See sample code:
var arr = [1, 2, 3];
arr.join('*') //"1*2*3"
- 2 If no parameters are specified, the default is to use
,to connect.
var arr = [1, 2, 3];
arr.join() //"1,2,3"
toString(): Returns the string form of the array
var arr = [1, 2, 3];
arr.toString() // "1,2,3"
valueOf(): Returns the array itself
var arr = [1, 2, 3];
arr.valueOf() // [1, 2, 3]
map():1 Call a function on all members of the array in sequence, and the return value is a new array.
var arr = [1, 2, 3];
arr.map(function(elem){
return elem*2;
});
//[2, 4, 6, 8]
arr; //[1, 2, 3]
- 2 The
mapmethod accepts a function as a parameter. When the function is called, the map method will pass in 3 parameters, namely the current member, the current position and the array itself (the last 2 parameters are optional).
arr.map(function(elem, index, arr) {
return elem * index;
});
//[0, 2, 6]
3 The
mapmethod can also accept a second parameter, which represents the object pointed to bythiswhen the callback function is executed.forEach(): Very similar to the map method, it also traverses all members of the array and performs some operation. Note: TheforEachmethod generally has no return value
var arr = [1, 2, 3];
function log(element, index, array) {
console.log('[' + index + '] = ' + element);
}
arr.forEach(log);
// [0] = 1
// [1] = 2
// [2] = 3
Note: The forEach method cannot interrupt execution and will always traverse all members. If you want to interrupt the traversal when certain conditions are met, use a for loop.
filter():1 Filter the elements of the array, and the return value is a new array composed of elements that meet the filter conditions.
var arr = [1, 2, 3, 4, 5];
arr.filter(function (elem) {
return (elem > 3);
});
//[4, 5]
- 2 The
filtermethod accepts a function as a parameter. When the function is called, thefitlermethod will pass in 3 parameters, namely the current member, the current position and the array itself (the last two parameters are optional).
var arr = [1, 2, 3, 4, 5];
arr.filter(function (elem, index, arr) {
return index % 2 === 1;
});
//[2, 4]
3 The
filtermethod can also accept the second parameter, which specifies thecontext object(iethis object) where the test function is located.some()andevery(): similar to “assert”, used to determine whether array members meet certain conditions.1 Accepts a function as a parameter, all array members execute the function in sequence, and return a
Boolean value. This function accepts three parameters, which are the member of the current position, the sequence number of the current position and the entire array.2 The
somemethod is that as long as the return value of one array member is true, the return value of the entire some method is true, otherwise it is false.
var arr = [1, 2, 3, 4];
arr.some(function (elem, index, arr) {
return elem >= 3;
});
// true
- 3 The
everymethod returns true only if the return values of all array members are true, otherwise false.
var arr = [1, 2, 3, 4];
arr.every(function (elem, index, arr) {
return elem >= 3;
});
// false
4 Note that for
empty array, thesomemethod returns false and theeverymethod returns true5 The
someandeverymethods can also accept a second parameter, which is used to bind thethis keywordin the function.reduce()andreduceRight(): Process each member of the array in turn, and finally accumulate a value.1
reduceis processed fromleft to right(from the first member to the last member)
arr.reduce(function(x, y){
console.log(x, y)
return x + y;
});
// 1 2
// 3 3
// 6
- 2
reduceRightis processed fromright to left(from the last member to the first member)
arr.reduceRight(function(x, y){
console.log(x, y)
return x + y;
});
// 3 2
// 5 1
// 6
Method to change the original array:
push(): Add several elements to the end of the array. The return value is the changed array length.
var arr = [1, 2];
arr.push(3) ;// 3
arr; // [1, 2, 3]
arr.push('b','c'); //5
arr; //[1, 2, 3, "b", "c"]
arr.push([10,20]); //6
arr; //[1, 2, 3, "b", "c", [10,20]]
pop(): Delete the last element of the array. The return value is the deleted element.
var arr =[1, 2, 3, "b", "c", [10,20]];
arr.pop(); //[10, 20]
arr; // [1, 2, 3, "b", "c"]
unshift(): Add several elements to the head of the array. The return value is the changed array length.
var arr = [1, 2];
arr.unshift(3,4 ); //4
arr; // [3, 4, 1, 2]
shift(): Delete the last element of the array. The return value is the deleted element.
var arr = ['a', 'b', 1, 2];
arr.shift(); //'a'
arr; //['b', 1, 2]
sort(): Array sorting.1 Note: The default is to convert all elements into
stringand then sort bystring Unicode code point. The return value is the new array.
var arr = [1, 2, 12, 'a', 'b', 'ab', 'A', 'B']
arr.sort(); // [1, 12, 2, "A", "B", "a", "ab", "b"] Note: 12 comes before 2
- 2 If the elements are all numbers and they need to be sorted from small to large, you can pass in a callback function as a parameter.
var arr = [1, 2, 12, 100]
arr.sort(function(a,b){
return a-b;
});
// [1, 2, 12, 100]
- 3 If you want to sort from large to small:
arr.sort(function(a,b){
return b-a;
});
//[100, 12, 2, 1]
reverse(): Reverse the position of elements in the array
var arr = [1, 2, 12, 'a', 'b', 'ab', 'A', 'B'];
arr.reverse();
//["B", "A", "ab", "b", "a", 12, 2, 1]
splice(): Modify array elements (add, delete, replace). Deleteseveralelements starting fromspecified index, and then addseveral elementsfrom that position. The return value is an array of deleted elements. Parameter 1 is the starting index of the element to be deleted, parameter 2 is the number of elements to be deleted, and the subsequent parameters are the elements to be added.1 Only delete, not add. You can pass in 2 parameters:
var arr = ['Alibaba', 'Tencent', 'Baidu', 'XiaoMi', '360'];
// Delete 3 elements starting from index 2
arr.splice(2, 3); // Return deleted elements ['Baidu', 'XiaoMi', '360']
arr; // ['Alibaba', 'Tencent']
- 2 Only add, not delete. The second parameter is set to
0, which means the element will not be deleted.
arr.splice(2, 0, 'Toutiao', 'Meituan', 'Didi'); // Returns [] because no elements were deleted
arr; //["Alibaba", "Tencent", "Toutiao", "Meituan", "Didi"]
- 3 First delete several elements, and then add several elements at the deleted positions.
var arr =["Alibaba", "Tencent", "Toutiao", "Meituan", "Didi"]
arr.splice(2,2,'Apple','Google'); //["Toutiao", "Meituan"]
arr; //["Alibaba", "Tencent", "Apple", "Google", "Didi"]
Array.isArray()
Used to determine whether a value is an array. If the parameter is an array, return true, otherwise, return false.
var arr = [1,2]
Array.isArray(arr); //true;
Array.isArray('a'); //false