JavaScript Learning Notes-Reference type Array

Source: Internet
Author: User

Two arrays

1. You can delete or create a new array item by using the Length property

arr = [1,2,3];arr.length = 4;//increase [1,2,3,undefined]arr.length = 2;//delete [up]
ARR[6] = 6; [1,2,3,undefined,undefined,6]

2. Detecting arrays

Array.isarray ()

3. Conversion method

Calls the ToString () method of the array, returning a comma-delimited string of strings of each value in the array. In fact, to create this string, the ToString () of each item of the array is called.

4. Stack method

The stack is a LIFO data structure, where data insertions and deletions occur at the top of the stack.

Push (), accept any number of arguments, add them one by one to the end of the array, and return the new array length (into the stack)

Pop (), delete the last element of the array, and return the value of the deleted item (out of the stack)

5. Queue method

The queue method is a FIFO data structure.

Shift (), delete the first item of the array and return the value of the item (queue header out)

Push (), inserting several items from the end of the array (queue trailing in)

Or

Unshift (), inserts several items from the head of the array, and returns the new Length (Queue header Entry)

Pop (), delete the last item in the array (the tail of the queue)

6. Reorder Methods

Reverse () and sort () return the sorted array

7. How to Operate

Concat () is used to connect two or more arrays. Finally, a one-dimensional array is returned. It does not change the value of the original array, it returns the copy

Slice () Creates a new array based on one or more items in the current array. Accepts 1 or 2 parameters. The first is the start position of the returned item, and the second is the end position

Splice () a powerful method. can be deleted, inserted, replaced. Accepts 3 parameters. Parameter 1, start position, parameter 2, number of items to delete, Parameter 3, item to insert. Parameters can also be passed as many as you want, followed by inserts into the array sequentially as items to be inserted

8. Location method

IndexOf (), LastIndexOf () accepts 1 parameters and can accept up to 2. Parameter 1, the value to find, and the index at which to find the start position of the parameter 2. Start the search from subscript 0 of the array. LastIndexOf () starts looking forward from the last position. Returns the position of the found value in the array, not found return-1

9. Iterative Methods

Each method receives two parameters. Parameter 1, the function that runs on each item; parameter 2, the scope object that runs the function

Every () each item in the array runs the given function, and if the function returns true for each item, the method returns True

Each item in the filter () array runs the given function, returning the array of items that the function returns True

ForEach () runs the given function for each item of the array, no return value

Map () runs the given function for each item of the array, returning an array that consists of the results of each function call.

Some () runs the given function for each item of the array, and returns True if the function returns true for either item

10. Merge method

Reduce (), Reduceright () both methods receive two parameters. Parameter 1, the function called on each item of the array, parameter 2, optional, as the initial value of the merge base. The function passed to reduce (), reduceright (), accepts 4 parameters. The previous value, the current value, the index of the item, and the array object. Any value returned by this function is automatically passed to the next item in the array as the first argument.

var values = [1,2,3,4,5];var sum = values.reduce (function (prev, cur, index, array) {    return prev + cur;}); alert (sum);//15

JavaScript Learning Notes-Reference type Array

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.