1. About Array
Array creation is very flexible. You can use the Array constructor or create an array "literal" directly.
var arr = new Array(); //[]var brr = Array(); //[] The two are equivalent to var arr = Array(3); //[]arr.length; //3 Empty array of length 3 var arr = Array(22,33,'qq',{}); //[22, 33, "qq", Object]var brr = [22,33,'qq',{}]; //Same as aboveArray is a built-in object for JavaScript. Yes, although it is an array, it is also an object! !
Use typeof to judge will return an Object! Array.isArray method can more accurately determine its type.
var a = [];typeof a; //objectArray.isArray(a); //true
2. Common methods
push() method
The push method can add one or more elements to the end of the array and return the length of the changed array!
Note: ① It returns the length of the array, not the array!
②This method will change the original array! !
var arr = Array(22,33,'qq',{});arr.push('weibo'); //5arr //[22, 33, "qq", {}, "weibo"]This is what we need to use when we want to merge two arrays
var a = [22,33];var b = [44,55];Array.prototype.push.apply(a, b)// or a.push.apply(a, b)// or a.push(44,55); //Array a = [22,33,44,55];
Be careful not to write the following! !
a.push(b);a; //[22,33,[44,55]]a.length; // 3 !!console.log(a); //[22, 33, Array[2]]
Writing it directly as a.push(b) will add b as an element to a, and it will not get the ideal effect!
If two object arrays now need to be merged like this:
var a = [ {name: 'Stark', value: 'Ironman'}, {name: 'Cap' , value: 'Oldman'}];var b = [ {name: 'Jerry', email: '[email protected]'}, {name: 'Lory' , email: '[email protected]'}, {name: 'susan', email: '[email protected]'}];//Error writing a.push(b); //3console.log(a); //[Object, Object, Array[3]]//Correct writing a.push.apply(ab); //5console.log(a); //[Object, Object, Object, Object, Object]pop() method
In contrast to push , it is to delete the last element of the array and return the deleted element:
var a = ['qq', 'weibo', 'weixin'];a.pop(); // 'weixin'a; // ['qq', 'weibo']
join() method
Separate the array by corresponding parameters and return as a string. If the parameter is empty, use ',' to separate it. This method does not change the original array:
var a = [1, 2, 3, 4];a.join(' ') // '1 2 3 4'a.join(' | ') // "1 | 2 | 3 | 4"var b = a.join() // "1,2,3,4"console.log(a); // [1,2,3,4]console.log(b); // "1,2,3,4"concat() method
It can combine multiple numbers and return a new array, but the original array remains unchanged:
var a = [22,33];var b = [44,55];var c = a.concat(b);console.log(a); //[22, 33]console.log(b); //[44, 55]console.log(c); //[22, 33, 44, 55]
var a = [{name: 'tom', email: '[email protected]'}, {name: 'peter', email: '[email protected]'}];var b = [{name: 'Jerry', email: '[email protected]'}, {name: 'Lory', email: '[email protected]'}, {name: 'susan', value: '[email protected]'}];var c = a.concat(b);c; // [{name: 'tom', email: '[email protected]'}, // {name: 'peter', email: '[email protected]'}, // {name: 'peter', email: '[email protected]'}, // {name: 'Jerry', email: '[email protected]'}, // {name: 'Lory', email: '[email protected]'}, // {name: 'susan', value: '[email protected]'}]map() method
map method will call a function on each member of the array in turn, returning a new array processed by the function, but the original array will not be changed!
var numbers = [1, 2, 3];var num = numbers.map(function (n) { // [2, 4, 6] return n * 2; });numbers; //[1,2,3] When the parameter of the function called by map method is one, this parameter represents the current member of the array; when the parameters are three, they are sequentially
The current member elem , index index , original array itself arr
var brr = [1, 2, 3].map( function(elem, index, arr) { return elem * index;});brr; // [0, 2, 6] map method can also accept a second parameter, indicating the object pointed to by this when the callback function is executed.
var arr = ['a', 'b', 'c'];var brr = [0, 2].map(function(e){ return this[e];}, arr)brr; // ['a', 'c'] In applications, sometimes when using ajax technology requires dynamically converting parameter arrays into a url request, map method will be very convenient, for example:
var b = [ {name: 'Jerry', email: '[email protected]'}, {name: 'Lory', email: '[email protected]'}, {name: 'susan', value: '[email protected]'}]; var url = b. map(function(n){ return n.name + "=" + n.email }) .join("&"); console.log(url); //[email protected]&[email protected]&[email protected]Then add the ip address, action and method before the url to complete the dynamic url splicing required by ajax, for example:
var endURL = "localhost:8080/XXXX/" + eventAction + "!" + operation + "?" + url;
Summarize
The above is the entire content of this article. I hope it will be of some help to everyone's study or work. If you have any questions, you can leave a message to communicate.