JavaScript Arrays Array

Source: Internet
Author: User

Note: Length of 1.array is not read-only. You can move an item from the end of the array or add new items to the array. Look at the following example:

var colors = ["Red", "yellow", "blue"];
Colors.length = 2;
Console.info (colors[2]);
Results: undefined
Cause: Setting the Length property to 2 moves out of the last item. If you set the length to be greater than the array length, each new item is a undefined value.

With length, you can easily add new items at the end of the array, such as:
var colors = ["Red", "yellow", "blue"];
Colors[colors.length] = "Gray";
Colors[colors.length] = "white";

ToString (), tolocalestring (), and valueof () differences:

ToString () is the use of strings instead of objects.
toLocaleString () is based on the different language environment bar object into a string, in fact, totolocalstring () is a default parameter, such as toLocaleString (' Ko-ka ') is based on the Korean language format conversion, display is the bird text.
ValueOf () is a method that gets the original value of an object, and the original value of the array is, of course, itself.

By default, array items are returned as a comma-delimited string, and a different delimiter can be used with the join () method. Such as:
var colors = ["Red", "yellow", "blue"];
colors = Colors.join ("| |");
Console.info (colors);
Results: red| | yellow| | Blue

First, the detection array
Question: How can I determine if an object is an array?
Answer: For a Web page or a global scope, the results can be achieved using instanceof.
if (value instanceof Array) {
}
instanceof acts on a single global execution environment. If a Web page contains more than one frame, there will actually be more than 2 global execution environments, and there are two different versions of the
constructor function. If you pass an array from one frame to another, the incoming array has a different construct than the one that was created natively in the second frame.
Function.
To solve this problem, ECMAScript added the Array.isarray () method to determine if it is an array. Regardless of which global execution environment it is.
Browsers that support the Array.isarray () method have ie9+,firefox4+ and chrome

Stack method
The stack is a LIFO data structure with the push () and pop () methods to implement a stack-like behavior.
Push () can receive any number of parameters, add it to the end of the array, and return the modified array length.
Pop () removes the last item from the end, reduces the length of the array, and returns the move out
Queue method
Queue: is a FIFO data structure, because push () can be added at the end, only requires a method of fetching items from the front of the array to implement the queue.
shift () is able to move out of the first item and return the item, with the array length minus 1.

Array Iteration methods:
Every (), filter (), ForEach (), map (), and some ()
Example:
var numbers = [1,2,3,4,5,4,3,2,1];
var everyresult = numbers.every (function (item, Index,array) {
Return (item > 2);
});
Console.info (Everyresult);

var someresult = numbers.some (function (item, Index,array) {
Return (item > 2);
});
Console.info (Someresult);

var filterresult = numbers.filter (function (item, Index,array) {
Return (item > 2);
});
Console.info (Filterresult);

var mapresult = Numbers.map (function (item, Index,array) {
Return (item > 2);
});
Console.info (Mapresult);

Result: False True [3, 4, 5, 4, 3] [False, False, True, True, True, True, True, False, false]
Every (): array passed in function must return true for each entry, this method returns True
Some (): Returns true whenever an incoming array function is satisfied
Filter (): A new array of data items that satisfy the condition in the array is returned
Map (): The operation is performed on the basis of the original array, and the result of each item is composed of a new array returned

JavaScript Arrays 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.