Array--An explanation of the property principle

Source: Internet
Author: User
Tags array length shallow copy

arrays (belonging to objects)---functions that they can implement are written by a function method
1, define:<1> can record a set of data <2> number from zero start
1. var array = [n/a];/* variable-declarative definition * /
2. var array=new array (+/-);/* The form of the object to define */
3. var array=[]; array[0] =1; array["name"] = "Harry";/* Parameter definition method * /
1th, 2 ways to define different methods 2 high efficiency reason: The 1th method is to detect the type first to help us create the new Array (three-way) object;   
arrays are subordinate to object type objects if not new first detects and then automatically helps us to create array objects
string is a basic type string type so it detects but does not create a string object

2. The function of Delete Push unshift three properties
1.delete arry[0]; there may be no value or null in the middle of the array of sparse arrays
2.arry.push (); Add an element to the end of the array
3.arry.unshift (); Add elements to the beginning of the array
4.arry.length-= 1; the second Delete method deletes the last array value

3. Output array:
1. For loop output array
<1>for (var i=0;i<n;i++) {Console.log (arr[i]);}
<2>for (var i in Array) {Console.log (arr[i]);}
<3> simple for loop only loops out the values inside the array contain sparse arrays but do not loop the object properties
for...in loop can loop out object properties can not be recycled sparse array     

4. Prototype refers to the object attributes that exist on the prototype chain.
1. Add an object property directly to the specified array var array = {}; array.prototype.x = type; (type can be a number or a string)
2. Add a method directly to the array Array.prototype.name = function name () {};
arrays are also formed by a combination of methods, using prototype to set the function of the desired method to specify the array.

5, the shallow copy of the array and the deep copy-------stack ===>> heap
1. If you want objects to not affect each other, you cannot use the equals sign because it points to the same heap, causing the change of one to affect the other
<1> If the array wants to solve this problem plus a concat can solve
Array 1 = array 2.concat ();
<2> If the object wants to solve this problem because the object has no concat attribute then we re-create an object by Object.assign
var arry = object.assign ([],hero)-----Copy Array
var Arry = object.assign ({},hero)-----Copy Object

6, sparse array
1. Sparse array True value is smaller than index value
[Empty,5,empty]
2. When creating a group, give a parameter so that the current array length is 5
var new_arry=new array (5);
3. The in parameter can be used to determine if the index has a value
Console.log (5 in New_arry);

7, two-dimensional array: var db_arry = new Array (1,2,3,new array ( ));
call: Db_arry[3][1]; generally by double for loop

//join converts an array to a string

var arry = [1,2,3,4];console.log (arry); var str = arry.join (); Console.log (str);

Eval converts a string into an array

var arry = "[1,2,3,4]"; Console.log (arry[0]);  Error Console.log (eval (arry) [0]);  

Concat stitching Array does not change the original array

var ay1=[1,2,3]; var new_a = ay1.concat[[3,4,5],[7,9,8]];console.log (new_a);    

Reverse reverse transfer Change original array

var a = [];console.log (A.reverse ()); Console.log (a);

Sort sort By default is from small to large to change the original array

var b = [4,5,2,3,1];console.log (B.sort ()); Console.log (b);

How to sort from big to small

1. The method will have two parameters a, b A for the following number, b for the previous number a-b>0 ascending, a-b<0 descending to change the original array

2. If the array contains Chinese characters or letters sorted by Aske code table

  function (A, b      ) {return a-b<0;  })

The index value (starting index value, end index value) that is connected to the slice slice does not change the original array

var ay=[1,2,3,4,5,6,7,8,9,10]; var // from the beginning of the fifth . // [6,9]Console.log (Ay.slice ( -1); Console.log (Y.slice (-3,3);    // no intersection cut out to null

Gets the first element and the last element

Console.log (Y.slice (0,1); Console.log (Y.slice (-1);

Splice Advanced Array function increment, delete change the original array
1. If splice gives a parameter then it can get an array from 0 to the specified index value in the array
2. If the splice gives two parameters then it is equivalent to cutting the specified position (index start, quantity)
3. If splice gives three parameters replace the array value of the first parameter to the second parameter with the third argument

[index start, index closed] both closed

varay=[1,2,3,4,5,6,8,7];ay.splice (5);//array from 0 to the specified index value(Ay.splice);//then it is equivalent to cutting the specified positionAy.splice ("a");//Replace the array value of the first parameter to the second parameter with the third argumentAy.splice (0,1);//intercept the first element of theAy.splice (0,1,100);//intercept the first element and replace it with aAy.splice (-5);//an array of values corresponding to 0 to 5Console.log (ay);//after the original array has changedvarAty= Ay.splice ("A", "B");//Replace the array value of the first parameter to the second parameter with the third argumentConsole.log (Aty);//Change the value

1, why the string is created, do not need a new string, while the general array is created by the need of the new array ();
Because arrays are subordinate to object type objects if not new first detects and then automatically helps us to create array objects
Ignoring string is the basic type of string so he only detects but does not create a string object
The process of general array new is the process of improving efficiency

When a string type is converted to a string object, you can use the new string;

2. Begin = Begin | | 0; If begin is not assigned a value, begin is 0;

3. Object freeze: This can be called in a function but cannot be assigned

Array--An explanation of the property principle

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.