- Four basic methods of JavaScript array & iterate

Fortunately, the airship will lose big at [expert group] + 1892213 give you a different small coup

An array of four basic methods are as follows:

The method described in Note
push () inserting one or more elements to the rearmost array and returns the result that the new length of the array will change the original array
pop () to delete the last element in the array, return the result of the change elements are deleted source array
the unshift () array in the front inserting one or more elements, return results for the new array can change the length of the original array
shift () Removes the first element in the array, return the result to the removed element will change the original array
method to iterate as follows:

The method described in Notes
for this cycle we know everything
forEach () and a for loop is similar to, but more needs to be compatible IE8 forEach () does not return a value. That is, its return value is undefined
filter () returns the result is true items, will form a new array. Can play a role of filtering does not alter the original array
map () for each primary array processing
every () if there is a return false, the mean traversal stops, each in claim returns true, the final result before returning to true
some () as long as there is a return to true, the traversal stops
four basic methods array (array element adding and deleting)
Push ()
Push (): inserting one or more elements to the rearmost array and returns the result for the new array length.

grammar:

The new length of the array .push = array (elements);
1
Code Example:

var arr = [ "Wang", "king", "three Wang"];

var result1 = arr.push ( "Wang Si"); // inserted at the end of an element
var result2 = arr.push ( "Wang Wu", "king six"); // multiple elements inserted end

console.log (result1); // print the results:. 4
the console.log (result2); // print the results:. 6
the console.log (the JSON.stringify (ARR)); // print the results: [ "Wang", " king "," King III "," King of four "," Wang five "," King of six "]

----------------
Disclaimer: This article is the original article CSDN bloggers "Yuanriver", and follow CC 4.0 BY-NC-SA copyright agreement, reproduced, please attach the original source and link this statement.
Original link: https: //blog.csdn.net/Yuanriver/article/details/102666251

POP ()
POP (): delete the last element in the array, it returns the result as the element is deleted.

grammar:

= Deleted element array .pop ();
. 1
Code Example:

var arr = [ "Wang", "king", "three Wang"];

was result1 arr.pop = ();

console.log (result1); // print result: Wang three
console.log (JSON.stringify (arr)); // print the results: [ "Wang", "king"]
. 1
2
. 3
. 4
. 5
. 6
the unshift ( )
the unshift (): in the front inserting one or more array elements returned result is the new length of the array. After inserting the elements, other elements of the index will in turn adjust.

grammar:

The new length of the array = array .unshift (element);
1
Code Example:

var arr = [ "Wang", "king", "three Wang"];

var result1 = arr.unshift ( "Wang Si"); // prepend an element
var result2 = arr.unshift ( "Wang Wu", "king six"); // prepend a plurality of elements

console.log (result1); // print the results:. 4
the console.log (result2); // print the results:. 6
the console.log (the JSON.stringify (ARR)); // print the results: [ "Wang Wu", " Wang six "," King of four "," Wang "," king "," King III "]

. 1
2
. 3
. 4
. 5
. 6
. 7
. 8
. 9
Shift ()
Shift (): Delete the first element in the array, return the result to the removed element.

grammar:

= Deleted element array .shift ();
. 1
Code Example:

var arr = [ "Wang", "king", "three Wang"];

var result1 = arr.shift();

console.log (result1); // print result: Wang
console.log (JSON.stringify (arr)); // print the results: [ "king", "three Wang"]
. 1
2
. 3
. 4
. 5
. 6
array traverse
through the array that is: Get and operation of each element in the array. In our combat development, the use of very frequently.

The method to iterate comprising: every (), filter (), forEach (), map (), some ()

PS: This method does not modify several original array.

Syntax:

Array / boolean / No = array .every / filter / forEach / Map / some (
function (Element, index, ARR) {
program and return values;
. 1
2
. 3
With these methods, we can replace some of the for loop. Here we turn to introduce.

loop iterates for
example:

var arr = [ "Millet", "rice" and the "m"];
for (var I = 0; I <arr.length; I ++) {
the console.log (ARR [I]); // ARR [I] represents each array element i
}

the console.log (ARR);
. 1
2
. 3
. 4
. 5
. 6
Printing results:

 

forEach () traversal
forEach () This method only supports traversing more than IE8 browser. IE8 and below browsers do not support this method. So if you need a compatible IE8, do not use the forEach, instead use a for loop to iterate can be.

forEach () method requires a function as a parameter. This function is created not by us but by our call, we call a callback function.

There are several elements in the array, the callback function will be performed several times. After machining, the browser will traverse to the elements.

The callback function passed three parameters:

The first parameter is currently traversing element

The second parameter index is currently traversing element

The third parameter is being iterated array

Code Example:

var arr = [ "Wang", "king", "three Wang"];

arr.forEach(function(item, index, obj) {
console.log("item:" + item);
console.log("index:" + index);
console.log("obj:" + obj);
console.log("----------");
});
1
2
3
4
5
6
7
8
打印结果:

item: Wang
index: 0
obj: Wang, Wang two, three king
----------

item: the king
index:. 1
obj: Wang, Wang two, three king
----------

item: Wang three
index: 2
obj: Wang, king, the king three
----------
. 1
2
. 3
. 4
. 5
. 6
. 7
. 8
. 9
10
. 11
12 is
13 is
14
Note that, forEach () return value is undefined. That is, it has no return value. If you try tempArry = arr.forEach () to receive such a way, it is not up to the effect.

filter ()
syntax:

Array.prototype.filter (function (Item, index) {})
. 1
Explanation: each item in the array operation callback function, the function returns a true result items, will form a new array (the return value is the new array ).

Example 1: Find the array element arr1 greater than 4, and returns a new array. code show as below:

where, R1 = [1, 3, 6, 2, 5, 6];

arr2 is arr1.filter = var (function (Item, index) {
return Item> 4; // arr1 greater than the return element 4
})
the console.log (arr2 is);

. 1
2
. 3
. 4
. 5
. 6
. 7
Printing results:

 

Example 2:

var arr1 = [ "Chongqing", "yuzhong", "northern", "Shapingba"];

arr1.filter arr2 = var (function (Element, index, Array) {
IF (element.length> 2) {// element in arr1, if more than two characters in length, and put it on my go arr2
to true return;
}
return to false;
});
the console.log (of arr1);
the console.log (arr2 is);

. 1
2
. 3
. 4
. 5
. 6
. 7
. 8
. 9
10
. 11
Results:

 

map () method
interpretation: for each item in the array operation callback function returns the result of this function, the new array of (new array is returned after processing).

For example, there is a known array arr1, I asked to make arr1 each prime plus 10, where you can use the map method. For example:

where, R1 = [1, 3, 6, 2, 5, 6];

arr2 is arr1.map = var (function (Item, index) {
return Item + 10; // make each element plus 10. arr1

})
The console.log (arr2 is);
. 1
2
. 3
. 4
. 5
. 6
. 7
Printing results:

 

Example 2:

var arr1 = [ "Chongqing", "yuzhong", "northern", "Shapingba"];

arr2 is arr1.map = var (function (Element, index, Array) {
return Element + "VAE"; // to all elements is increased arr1 string "vae", placed in arr2 is.
});

the console.log (of arr1);
the console.log (arr2 is);
. 1
2
. 3
. 4
. 5
. 6
. 7
. 8
Results:

 

every () method
to explain: each item in the array run a callback function, if the return true, every will return true; if there is a return false, then stop the walk, this method returns false.

Note: Returns the value of every () method is a boolean parameter is the callback function.

For example:

var arr1 = [ "Chongqing", "yuzhong", "northern", "Shapingba"];
var = bool1 arr1.every (function (Element, index, Array) {
IF (element.length> 2) {
return to false;
}
to true return;
});
the console.log (bool1); // output: false. As long as there is an element length of more than two characters, it returns false

var arr2 = [ "Chongqing", "yuzhong", "northern", "Shaping"];
var = a bool2 arr2.every (function (Element, index, Array) {
IF (element.length> 2) {
return to false;
}
to true return;
});
the console.log (a bool2); // output: true. Because the length of each element are the two characters.
----------------

Guess you like

Origin www.cnblogs.com/java67/p/11728552.html