Developed a method commonly used to explain the operation of the array

. 1, the concat ()
the concat () method for connecting two or more arrays. This method does not change the existing array, only return a copy of the array is connected.
of arr1 = var [l, 2,3];
var arr2 is = [4,5];
var = arr1.concat ARR3 (arr2 is);
the console.log (of arr1); // [. 1, 2,. 3]
the console.log ( ARR3); // [. 1, 2,. 3,. 4,. 5]
2, the Join ()
the Join () method for all elements in the array into a string. Elements are separated by a specified delimiter, default ', the' division without changing the original array.
ARR = var [2,3,4];
the console.log (arr.join ()); // 2,3,4
the console.log (arr.join () '.'); //[2.3.4]
. 3, Push ()
Push () method to add one or more elements to the end of the array, and returns the new length. Added at the end, returns the length, it will change the original array.
A1 = var [2,3,4];
var a1.push A2 = (. 5);
the console.log (A1); // [2,3,4,5]
the console.log (A2); //. 4
. 4 , POP ()
pop () method removes and returns the last element of the array. Returns the last element will change the original array.
ARR = var [2,3,4];
the console.log (arr.pop ()); //. 4
the console.log (ARR); // [2,3]
. 5, Shift ()
Shift () method is used the first element of the array from which to delete, and returns the value of the first element. Returns the first element, change the original array.
ARR = var [2,3,4];
the console.log (arr.shift ()); // 2
the console.log (ARR); // [3,4-]

. 6, the unshift ()
the unshift () method to add one or more elements to the beginning of the array, and returns the new length. Returns the new length, change the original array.
ARR = var [2,3,4,5];
the console.log (arr.unshift (10,100)); //. 6
the console.log (ARR); // [10, 100, 2,. 3,. 4,. 5]

. 7, Slice ()
Returns a new array containing elements from start to end (not including the element) in the arrayObject. Returns selected elements, the method does not modify the original array.
ARR = var [2,3,4,5];
the console.log (arr.slice (l, 3)); // [3,4-]
the console.log (ARR); // [2,3,4, 5]

. 8, splice ()
splice () method can remove zero or more elements from the beginning of the index, and to replace those removed element with one or more values of the parameters declared in the list. If you remove an element from arrayObject, then it returns an array containing the elements to be deleted. splice () method will be modified directly on the array.
Syntax: Array.splice (index, howmany, ITEM1, ..., inside `)
Index Required. Where the provisions of the Add / Remove elements
howmany necessary. Provision should be deleted how many elements
item1 optional. To add a new element to the array
var A = [5,6,7,8];
the console.log (a.splice (1,0,9)); // []
the console.log (A); // [ . 5,. 9,. 6,. 7,. 8]
var B = [5,6,7,8];
the console.log (b.splice (l, 2,3)); // [. 6,. 7]
the console.log ( b); // [5, 3 , 8]

. 9, the substring () and substr ()
the same point: If only a write parameter, both are the same effect: since all the current is taken from the string until the final subscript string string fragment.
substr (startIndex);
the substring (startIndex);
var STR = '123456789';
the console.log (str.substr (2)); // "3456789"
the console.log (str.substring (2)); // "3456789 "
different points: the second parameter
substr (startIndex, lenth): the second parameter is taken string length (character string of a length from the starting point taken);
the substring (startIndex, endIndex): the second parameter is taken subscript final string (character string taken between the two positions, 'containing free end of the head').
the console.log ( "123456789" .substr (2,5)); // "34567"
the console.log ( "123456789" .substring (2,5)); // "345"

10, Sort sorted
according to Unicode code positions sorted in ascending order by default
var A = [ 'Red', 'Blue', 'Green'];
a.sort (); // [ "Blue", "Green", "Red"]
NUM = var [. 1, 10, 21 is, 2];
num.sort (); // [. 1, 10, 2, 21 is]
num.sortNumber (); // [1,2,10,21]

. 11, Reverse ()
Reverse () method to reverse the order of elements in the array. It returns an array after reversed changes the original array.
ARR = var [2,3,4];
the console.log (arr.reverse ()); // [. 4,. 3, 2]
the console.log (ARR); // [. 4,. 3, 2]

12 is, the indexOf and lastIndexOf
accepts two parameters: the value found, to find the starting position
does not exist, return -1; present, the return position. indexOf is a look from front to back, lastIndexOf is to look from the back.
the indexOf
var A = [2,. 9,. 9];
a.indexOf (2); // 0
a.indexOf (. 7); // -1
IF (a.indexOf (. 7) === -1)} {
lastIndexOf
Numbers = var [2,. 5,. 9, 2];
numbers.lastIndexOf (2); //. 3
numbers.lastIndexOf (. 7); // -1
numbers.lastIndexOf (2,. 3); //. 3
numbers.lastIndexOf ( 2, 2); // 0
numbers.lastIndexOf (2, -2); // 0
numbers.lastIndexOf (2, -1); //. 3

13 is, Every
each run a given function of the array, each of which returns ture, true is returned
function Fun (Element) {
return Element <10;
}
[2,. 5,. 8,. 3,. 4]. every (fun); // true

function fun(element) {
return element < 10;
}
[2, 5, 11, 3, 4].every(fun); // false

14, filter
arrays for each item in the array given a run function returns a value ture composed
var ARR = [1,2,3,4,5];
var A = arr.filter (function (Item ) {
return Item =. 3;!
});
the console.log (A) // [1,2,4,5]

15, forEach array traversal
var ARR = [. 1, 2,. 3];
var arr2 is = [];
arr.forEach (function (Item) {
arr2.push (Item +. 1)
});
the console.log (arr2 is) // [2, 3, 4]
array method
arr.push () in the array in the last parentheses contents
arr.pop () in the array to remove the final last
arr.sort () arr.sort (function (a , b ) {return ab}) is the ascending order in descending order ba
arr.length returned array length
arr.shift () delete from a first array
arr.unshift () parentheses added from a first array
arr .join () array in accordance with the brackets input into a string
, such as brackets are '-' is 1-2-3-4-5
arr.reverse () array reversed
as 1,2,3,4 , 5, 5,4,3,2,1,
arr.concat () the original contents of the array passed in parentheses and you synthesize a new array does not change the original array
, such as: arr.concat (0,0,0 ) becomes the array (ARR, 0,0,0)
arr.some () satisfying some of the conditions are true
arr.some (function (ELE) {IF (ELE>. 4) {}} return to true) to true
arr.every () satisfies all the conditions before returning to true
arr.some (function (ELE) {IF (ELE> 0) { to true}} return) to true
arr.splice ()
arr.indexOf () detecting whether the contents of brackets in the array, the corresponding index is returned, not -1 is returned
arr.lastIndexOf () returns the parentheses in the array parameter the last occurrence of the corresponding index
(function (ele, index, arr ) {arr.forEach
function body
}) iterate
arr.filter (function (ele, index) {if (age> 14) {return true}}) filter array
arr.map (function (ele, index) {return ele + 1}) for the selected arithmetic array
arr.reduce (function (prev, cur, curIndex, arr) {return prev + cur}, init) ( string splicing)
the init is the initial value of the presence prev, if the first bit is not present in the array
prve + cur can, prev and cur plus sign may also be connected to the intermediate array prev + '-' + cur
[ other methods for ES6 see another array an article ]

发布了5 篇原创文章 · 获赞 0 · 访问量 65

Guess you like

Origin blog.csdn.net/weixin_42602873/article/details/104667835