Summary of Array object usage examples in JavaScript

高洛峰
Release: 2016-12-03 15:20:35
Original
1349 people have browsed it

The examples in this article describe the usage of Array objects in JavaScript. Share it with everyone for your reference, the details are as follows:

Array array object has many commonly used methods and attributes, which are summarized as follows:

1. The length attribute gets the number of elements in the array.

2. The concat() method connects two arrays. Concatenate two arrays. An example is as follows:

var names= new Array('Jack','Tom','Jim'); var ages= new Array(12,32,44); var concatArray; concatArray=names.concat(ages);
Copy after login

The concatArray here is a new array that combines the name array and the age array.

3. The slice() method obtains some array elements in the array.

Generally there are two parameters, the first represents the starting position, and the second represents the ending position (similar to substring). It is worth noting that the intercepted array element is located before the second parameter position. In other words, if the second parameter is 4, it means intercepting before the fourth array element.

4. The join() method converts the array into a string. This method is a javascript method and is often used in jQuery. The example is as follows:

var myShopping=new Array("eggs","apple","milk"); var myShoppingList = myShopping.join("
"); document.write(myShoppingList);
Copy after login

The myShoppingList here becomes a string, the content is "eggs
apple
milk";

5. The sort() method sorts the elements in the array. They are arranged in alphabetical order, from smallest to largest.

6. The reverse() method flips the elements in the array and turns them around.

If you combine the sort() method with the reverse() method, you can achieve the effect of sorting in reverse order.

That is, sort first and then turn over, so as to achieve the effect of reverse order.

The following is a small comprehensive example:

Copy after login

This small example is a small example of using the sorting method, flipping method and join method in the array object. If the input is 1, it will be sorted in order and output. If it is -1, it will be sorted in reverse order and output.


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!