Home >Web Front-end >JS Tutorial >Detailed explanation of JavaScript arrays and loops_javascript skills

Detailed explanation of JavaScript arrays and loops_javascript skills

WBOY
WBOYOriginal
2016-05-16 16:02:081218browse

An array is an ordered combination of elements. In JavaScript, arrays can be created using formal object notation, or they can be initialized using literal notation.

Copy code The code is as follows:

var arrObject = new Array("val1", "val2"); // Array as object
var arrLiteral = ["val1", "val2"]; // Array literal

For developers, there is no difference: an Array method can be called on both literals and objects. For the JavaScript engine, an array literal must be reinterpreted every time it is accessed, especially when used within a function.

Use the new operator to create a new Array object:

Copy code The code is as follows:

var arrObject = new Array();

You can also create a new array with certain values:
Copy code The code is as follows:

var arrObject = new Array("val1", "val2");

Arrays in JavaScript are indexed from 0, which means that the index of the first element is 0 and the last element is the length of the array minus 1.

1. Loop through the array

Problem: Want to easily access all elements of an array.

Solution:

To access an array, the most common way is to use a for loop:

Copy code The code is as follows:


Discussion:

The for loop can be used to access each element of the array. The array starts at 0, and the array property length is used to set the end of the loop.

2. Store and access values ​​in order

Problem: Want to store values ​​in such a way that they can be accessed sequentially the way they are stored;

Solution:

To store and access values ​​in the order they are received, create a first-in, first-out (FIFO) queue. Use the push method of the JavaScript Array object to add items to the queue, and use shift to get the items:

Copy code The code is as follows:


Discussion:

The Array push method creates a new array element and adds it to the end of the array:

Copy code The code is as follows:

queue.push("first");

Each time an element is pushed, the count of array elements is incremented.

The Array shift method extracts an array element from the front of the array, deletes it from the array, and returns the element:

Copy code The code is as follows:

var elem = queue.shift();

For each element of the shift operation, the array element will be decremented, because shift not only returns the item, but also modifies the array.

3. Store and access values ​​in reverse order

Problem: I want to store values ​​in a way that accesses the values ​​in reverse order, accessing the most recently stored value first, which is a last-in-first-out (LIFO) stack.

Solution:

To store values ​​in reverse order, create a LIFO stack. Use the push method of the JavaScript Array object to add items to the stack, and the pop method to get items:

Copy code The code is as follows:



Discussion:

The stack is also an array, where each newly added element is at the top of the stack and is obtained in last-in-first-out order.

The Array push method creates a new element and adds it to the end of the array:

Copy code The code is as follows:

stack.push("first");

Each time an element is pushed, the array element count is incremented.

The Array pop method extracts an array element from the tail of the array, removes it from the array, and returns the element:

Copy code The code is as follows:

var elem = stack.pop();

Every time an element is popped, the array element count will be decremented, because popping also modifies the array.

4. Search in the array

Question: I want to search for a specific value in an array and, if found, get the index of the array element.

Solution:

Use the new (ECMAScript 5) Array object methods indeOf and lastIndexOf:

Copy code The code is as follows:


Although browsers sometimes support both indexOf and lastIndexOf, this is only formalized in the ECMAScript 5 version. Both methods accept a search value, which is then compared to each element in the array. If the value is found, both methods return an index into the array element. If no value is found, -1 is returned. indexOf returns the first element found and lastIndexOf returns the last element found.

See:

Not all browsers support indexOf and lastindexOf. The solution for this function:

Copy code The code is as follows:


 5、对每个数字元素应用一个函数

  问题:想要使用一个函数来检查一个数组值,如果满足给定的条件,就替换它。

  解决方案:

  使用新的ECMAScript 5 Array对象的forEach方法,来针对每个数组元素都绑定一个回调函数:

复制代码 代码如下:


讨论:

  forEach方法接受一个参数,这个参数是个函数。该函数自身有3个参数:数组元素,元素的索引和数组。

  参见:

  大多数浏览器都支持forEach。然而,对于那些不支持的浏览器,可以使用Array.prototype属性来模拟forEach行为。

复制代码 代码如下:


6. Create a filtered array

Question: I want to filter the values ​​of elements in an array and assign the results to a new array.

Solution:

Use the filter method of the Array object:

Copy code The code is as follows:


Discussion:

The filter method is a newly added method in ECMAScript 5, which applies a callback function to each array element. The function passed as a parameter to the filter method returns a boolean value, true or false, based on the result of testing the array element. This return value determines whether the array element is added to a new array. If the function returns true, it will be added; otherwise, it will not be added.

See:

For simulation implementation of browsers that do not support the filter method:

Copy code The code is as follows:


7. Verify array content

Problem: Want to ensure that an array meets a certain condition.

Solution:

Use the every method of the Array object to check each element of the given condition.

Copy code The code is as follows:



Discussion:

The every and some methods of the Array object are both the latest ECMAScript 5 Array methods. The difference is that when using the every method, as long as the function returns a false value, the processing will end and the method returns false. The some method will continue to test each array element until the callback function returns true. At this time, other elements are no longer verified, and this method returns true. If the callback function tests all elements and does not return true at any time, some method returns false.

See:

Implementation method for browsers that do not support every and some:

Copy code The code is as follows:


Statement:
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