Summary of Javascript Array

Source: Internet
Author: User

Summary of Javascript Array

In several reference types of JavaScript (Object, Array, Date, RegExp, Function, basic reference type), the Array type should be the most applied type except for the object. Unlike other languages, arrays in ECMAscript (JavaScript) are not strongly defined, that is, each item in the JavaScript array can store any object.

 

var test=['alpha','beta',3,false];alert(test);//['alpha','beta',3,false]

 

① Array Construction:

There are two ways to build arrays in js:

1. Use the constructor of the array

var test=new Array();

You can specify the size of the array or the elements in the array.

Var test1 = new Array (3); // specify the size of var test2 = new Array ('demo1', 'demo2', 222); // specify the content

1. array literal representation:

 

var test=['alpha','beta',3,false];

 

The length attribute of the js array is not a read-only attribute, that is, we can modify the length of the array at will:

 

Var test = new Array ('demo1', 'demo2', 333); console. log (test. toString (); // 'demo1', 'demo2', 333 test [3] = 'am 4'; console. log (test. toString (); // 'demo1', 'demo2', 333, I am the fourth

 

In this way, no error is reported. With this attribute, we can always fill the value at the end of an array. The maximum length of the array is 4294967295, which basically meets all programming requirements.

 

② Monitoring Array

How can I determine whether an object is an array?

Use the keyword instanceof and Array. isArray ();

Var test = new Array ('demo1', 'demo2', 333); if (test instanceof Array) {alert (test is a array ); // test is a array} // This method supports ie9 + if (Array. isArray (test) {alert (test is a array); // test is a array}
③ Conversion Method

Almost all objects have the toString (), valueOf (), toLocaleString () method, and the toString () method of the array is called () returns a comma-separated string that is concatenated by each item in the array. Generally, toString () and toLocaleString () return the same value, but the application may have detailed issues. refer to the following code:

var person1 = {toLocaleString: function() {return alpha;},toString: function() {return Beta;}}var person2 = {toLocaleString: function() {return alpha;},toString: function() {return Alpha;}}var people = [person1, person2];alert(people);alert(people.toString());alert(people.toLocaleString());

Here, two objects person1 and person2 are defined, and a toString and toLocaleString method are defined for the two objects respectively. The two methods return different values. An array is created to store two objects. When the array is passed to alert, the output result is Beta and Alpha. This is because the toString method is called implicitly, the call toString is also displayed later. When the array calls toLocaleString, the output is alpha and alpha, because the toLocaleString method that can be used for each array item is called.

By default, the toString, toLocaleString, and valueOf methods all return array items in the form of strings separated by commas. If you do not want to use commas, you can use the join method.

var people=['alpha','Beta',3,'names'];console.log(people.join('||'));

If one of the arrays is null or undefined, The results returned in the preceding conversion method are all null strings.

④ Stack method

In JavaScript, Arrays can be used as stacks. stacks are LIFO data structures. That is to say, the newly added elements are removed first, and all the elements are popped up, push operations occur in a place called the top stack. The push () and pop () methods are provided in JavaScript to complete stack operations.

 

var newArr=[1,2,3,4,5];newArr.push(6);alert(newArr.toString());//1,2,3,4,5,6newArr.pop();alert(newArr.toString());//1,2,3,4,5

 

⑤ Queue Method

Since it can be used as a stack, it can also be used as a queue. The queue rule is FILO, that is, it is advanced and later. JavaScript provides shift () and push () for this operation.

var newArr=[1,2,3,4,5];newArr.shift();alert(newArr.toString());//2,3,4,5newArr.push(6);alert(newArr.toString());//2,3,4,5,6

JavaScript also provides an unshift method, that is, adding an element before the queue and adding 1 to the length.

 

var newArr=[1,2,3,4,5];newArr.shift();alert(newArr.toString());//2,3,4,5newArr.unshift(6,7);//6,7,2,3,4,5alert(newArr.toString());

 

Using pop, push, shift, and unshift together, you can implement many methods, such as reverse queue operations.

⑥ Re-sorting method

The array itself provides two reverse (reverse) and sort (reverse) methods.

var newArr=[1,2,3,4,5];var secArr=[1,3,5,7,2,4];newArr.reverse();secArr.sort();alert(secArr.toString());//1,2,3,4,5,7alert(newArr.toString());//5,4,3,2,1

With regard to the sort method, it seems that there is no problem, but the actual implementation of the sort method is to call the toString () method of each array item. That is to say, sort is sorted by comparing the string size.

var secArr=[1,3,5,7,2,4,11];secArr.sort();alert(secArr.toString());//1,11,2,3,4,5,7

In most cases, this sorting method does not meet the requirements. What should we do? In fact, the sort () method provides a reference comparison function. This function is used to input two parameters. If the first parameter is located before the second parameter,-1 is returned, if the return value is equal to 0 and 1 is returned after it is located, we only need to rewrite this method and input sort () to complete the sort operation in our own way.

var secArr=[1,3,5,7,2,4,11];secArr.sort(compare);alert(secArr.toString());//1,2,3,4,5,7function compare(value1,value2){return value1-value2;}
 


 





 















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.