Array of JavaScript (i)

Source: Internet
Author: User

In JavaScript, objects, arrays, functions are the most commonly used things, finished writing objects and functions, finally, the array bar, refers to the array, you can only think, Map,foreach Ah, Pop,push ah, when really not a little bit of accumulation? So many years Cheng when really just learned for the loop, life so beautiful, you are so shallow ignorant ah, pity ah pity! If you really think so, you are an idiot. Don't say much nonsense, the dry goods go up!

First, the array element can be any type, the index is zero-based, the array may be sparse, that is, the index of the array element is not necessarily continuous oh, the middle can have a vacant yo. Then the length of the array is for the sparse array, and length is greater than the number of all elements.
An array is a special form of an object, so its index is actually the same as the property name that happens to be an integer. However, using numeric indexes to access array elements is generally much faster than accessing regular object properties.

The array inherits the attributes from the Array.prototype.
1, creating an array
Here's an uncommon way to create

var undefs = [,,]undefinedundefs (2) [emptyx2]
var New  varnew Array (varnew Array (1,2,3,4, ' thank you ')

Javascipt converts the specified numeric index value to a string----the index value 1 to "1" and then uses it as the property name
var o = {};
O[1] = "one";

The special thing about arrays is that they update their length property values as needed.
Note: You can use a negative or non-integer to index an array. In fact, an array index is just a special type of object property name.
2, sparse array, length
An array that contains a discontinuous index starting at 0.

var New Array (5); a[] = 0; 0A (1001) [emptyx1000, 0]

Length A special thing is, look at the following example:

var a = [1,2,3,4,5= 3; 3A (3) [1, 2, 3= xx=A (5) [emptyx5]

3: Array additions and deletions

Arr.push (' One ', ' both ');//Trailing insert

Arr.unshift ()//Header Insert

Delete A[1]//deletion does not affect the original length of the array, and the original array becomes a sparse array after deletion.
Pop ()//tail Delete an element
Shift ()//head Delete an element

Splice () is a generic method of inserting, deleting, and replacing an array of operations.

4 Traversal of an array

Commonly used for loops, you cannot use the For/in loop to detect an array, because the for/in loop can enumerate inherited property names, such as methods added to Array.prototype. Unless you use an additional detection method to filter unwanted properties.

5. Methods of Arrays:

join (), reverse (), sort (), concat (), slice (), splice ()
tostring ()
new array method to traverse, map, filter, detect, simplify and search the array.
every () and some () do not always access each array element, and if there is fasle, it terminates the traversal as early as possible .

 var  a = [2,12,3,5,6 function  (v,i,a) {A[i] = V+1}) A.map ( function  (x) {return  x*x}) ; A.filter ( function  (x) {return  x <3}) //  The returned array element is a subset of the called Array.  a.every (function  (x) {return  x< //  All values <10  a.some (function  (x) {return  x<10}) //  There is a number less than 10 is true  

Focus on the less commonly used reduce function:

var sum = a.reduce (function (x, y) {return x+y},0);requires two parameters, first one is a function, the task is to use a method to reduce the value of two to a value, and return the simplified value, the second argument is passed to the function of the initial value. In the example, the parameters for the first call to the degenerate function are 0 and 2, the sum is returned 2, the second call is 2 and 12, the return is 14, each time the last computed return value and the next calculation of the array. Finally, reduce () returns this value. When you call reduce () without specifying an initial value, it takes the first element of the array as its initial value. The above summation example can omit the initial value setting. an empty array call to the reduce function will error!!! Reduceright () works like reduce (), except that the array is processed from high to low (from left to right) by the array's index. For example, calculation: 2^ (3^4), which is calculated from right to left.
var a = [2,3,4var result = A.reduceright (function(x, y     ) {return  Math.pow (y,x);})

The Union function here is written by itself, and it looks like there is no problem in running it, you can merge object properties:

var objects = [{x:1},{y:2},{z:3function  Union (x, y      ) {for (var   in y) {          innull : x[i] = y[i];      }       return  var merged = objects.reduce (union);

The Last Method: IndexOf () and LastIndexOf (), the inside value is to retrieve the field, no return-1, there is the return index, one from the back looking, one from backward looking. Nothing to say. End of this article!!!

Array of JavaScript (i)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.