The knowledge points commonly used in arrays array in JS

Source: Internet
Author: User

Properties and methods of arrays

Property

Sets or returns the length of an array ary.length

Empty array: ary.length = 0;

Method (ary. method)

1. Add, delete

1) Add Ary.push at the end (added elements, multiple uses are well separated); Returned is the length

1) The opening increases ary.unshift (added elements, multiple are separated with good); Returned is the length

2) Delete Ary.pop () at the end; Only one element at the end can be deleted, and the deleted element is returned.

2) Start delete ary.shift (); Only one element at the end can be deleted, and the deleted element is returned.

3) Delete or add elements at the specified location

Ary.splice (the subscript of the position, the number of elements removed, the element added, the element added, and the elements added);
The first two parameters must have

2. Find

1) ary.includes (' a '); Determine if the element exists in the array

2) Ary.indexof (' a '); Find the position of ' a ' in the array and return to the subscript

3) Ary.lastindexof (' a '); Find ' A ' in the last position in the array, return to subscript

Use IndexOf (' a ') and LastIndexOf (' a ') to determine whether the element is unique in the array.

3. Intercepting a new array (slice)

Ary.slice (start subscript, end subscript);

A new array is formed as part of the Intercept array, and the new array is truncated from the beginning, and truncated to the previous element of the end subscript.

4. Splicing of array elements

Ary.join (' Symbols for stitching ');

Each element of an array is stitched together by a specified symbol to form a new string

5. Sorting (sort)

Ary.sort (function (b) {
return b-a; Sort from big to small
});


Traversal of an array

1. Iterating through the array with a For loop

for (Let I=0;i < ary.length; i++) {
Console.log (Ary[i]); Each item that corresponds to an array of small marks
}

2.for...of traversing an array

For (let item of ary) {
Console.log (item); Each item in the array
}

3.forEach method Traversal Array

Ary.foreach (function (item,index) {
Console.log (Item,index); Item must be filled in, index optional
});

The knowledge points commonly used in arrays array in JS

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.