Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

JavaScript array traversal and what are the methods for dealing with sparse arrays

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article introduces the "javaScript array traversal and sparse array processing methods" related knowledge, in the actual case operation process, many people will encounter such a dilemma, and then let the editor lead you to learn how to deal with these situations! I hope you can read it carefully and be able to achieve something!

1.forEach () method

The    forEach (parameter 1, parameter 2) method itself takes two arguments, the first of which is a function, and each element in the array calls this function once (except for the sparse array). Generally speaking, we will only pass in the first parameter (and is required), and we will not introduce any other parameters here. The parameter passing of the following other methods is the same as that of forEach ().

   and in the following method, the function passed as an argument also has its own parameter values, which are the value of the array element, the index of the array element, and the array itself. That is:

MyArray.forEach (

Function (value of array element, index of array element, array itself) {

}

)

Add: function (a) {return aqui3} is an anonymous function without a function name, which can be written in the form of an arrow function

A = > axi3

Example:

Let myArray = [1pm 2pm 3]

/ / num= > {} Arrow function, which is equivalent to an anonymous function

/ / function (num) {num = num * 3}

MyArray.forEach (num = > {

Num = num * 3

});

Console.log (myArray) / / print the result as [1,2,3]

The    forEach () method does not return a value, and there is no break statement that terminates the iteration like a normal for loop, and as mentioned above, the changes we make to num here do not change to the myArray array.

2.map () method

The difference between the    map () and the forEach () method is that the first parameter function of the map () method takes the elements of each of our arrays and can return a value. The return value of the entire map () method is an array of the return values of this function.

   example:

Let myArray = [1pm 2pm 3]

Let result = myArray.map (num = > {

Num * = 3

Return num

});

Console.log (result,\ nprimitive array, myArray)

/ / the print result is

/ / [3, 6, 9]

/ / original array [1, 2, 3]

3.filter ()

The    filter () method also has a return value, but he expects the return value of the parameter function to be of type boolean, that is, it expects the parameter function to be an assertion function. Its return value is an array of elements in myArray that meet the criteria.

Example:

Let myArray = [1pm 2pm 3]

Let result = myArray.filter (num = > {

Num * = 3

Return num > 5

});

Console.log (result)

/ / [2, 3]

   we can see that the numm values are 3, 6, 9 after operation, where 6 and 9 satisfy the condition greater than 5. The return value is the two elements 2 and 3 in the array, not the result of the operation.

4. Some very useful methods

Find () and findIndex ()

The    methods are similar to filter (), but they stop iterating when they find the first value that meets the condition, the find () method returns the value of the element, and the findIndex () method returns the index of the element in the array. Example:

Let myArray = [1pm 2pm 3]

Let result = myArray.find (num = > {

Num * = 3

Return num > 5

});

Console.log (result) / / print value: 2

Let myArray = [1pm 2pm 3]

Let result = myArray.findIndex (num = > {

Num * = 3

Return num > 5

});

Console.log (result) / / print value is: 1

Every () and some ()

The    every () and some () functions return boolean and also use an assertion function as the first input parameter, but every () returns true only if all elements in the array meet the condition of the assertion function, and every () stops traversing and returns false when one element does not meet the condition.

Let myArray = [4, 1, 2, 3]

Let count = 0; / / used to record the number of traverses

Let result = myArray.every (num = > {

Count++

Num * = 3

Return num > 5

});

Console.log (result, "number of traverses", count)

/ / print value: the number of times false traverses is 2

   while the some () function stops traversing and returns the true value as long as one condition is met.

Let myArray = [4, 1, 2, 3]

Let count = 0; / / used to record the number of traverses

Let result = myArray.some (num = > {

Count++

Num * = 3

Return num > 5

});

Console.log (result, "number of traverses", count)

/ / print value: the number of times true traverses is 1

What    needs to note here is that the elements of the myArray array are multiplied by 3, and the results are 1, 6, 9, where 6 and 9 meet the conditions greater than 5, but they return elements in myArray, that is, 2 and 3 without operation. Elements that do not meet the criteria are not returned, unlike map (), where the array length returned by map () is the same as the original array. )

Processing of sparse arrays

   sparse array definition:

A sparse array is an array whose elements do not have an index starting at 0. Normally, the length attribute of an array indicates the number of elements in the array. If the array is sparse, the value of the length attribute is greater than the number of elements.

A normal array of   , such as let myArray = [0Power1], has a length of 3, the number of elements is 3, which is the same as the length of the array, and the index is 0, 1, and 2, respectively.

Simple practice of    sparse array:

Let testArray = new Array (10); / / the array length is 10

TestArray [20] = 1 / / Array length is 21

Console.log (testArray) / / print the result as [, 1]

   the length of the array is 21 and there is only one element, which becomes a sparse array. Practical scenarios that create sparse arrays may occur when delete is used to delete array elements, such as:

Let myArray = [1pm 2pm 3]

Delete myArray [1]

Console.log (myArray, "Array length is", myArray.length)

Console.log (1 in myArray)

/ / the print result is

/ / the length of the array is 3.

/ / whether there is a false for the element with index 1

   delete does not change the length of the array, nor does it change the index values of other elements, so it forms a sparse array. If you want the element index value to change with the deletion of the array elements, you can use the splice () method.

The forEach () method does not traverse missing elements in a sparse array

Let myArray = [4 Jing Ji 2 Ji 3]

Console.log ("myArray", myArray)

/ / print value: myArray [4, 2, 3]

Let count = 0; / / used to record the number of traverses

MyArray.forEach (num = > {

Count++

});

Console.log ("Array length is", myArray.length, "number of iterations", count)

/ / the length of the array is 4 times, and the number of times is 3.

Map () also does not traverse the missing elements in the sparse array, but its array of returned values is the same length as the original array, and the missing elements are in the same position.

Let myArray = [4 Jing Ji 2 Ji 3]

Console.log ("myArray", myArray)

/ / print value: myArray [4, 2, 3]

Let count = 0; / / used to record the number of traverses

Let result = myArray.map (num = > {

Count++

Return num*3

});

Console.log ("Array length is", myArray.length, "number of traverses is", count, "returned array is", result)

/ / print value: the length of the array is 4

/ / the number of traverses is 3

/ the returned array is [12, 6, 9]

The return value of filter () does not contain missing elements in the sparse array

Let myArray = [4 Jing Ji 2 Ji 3]

Console.log ("myArray", myArray)

/ / print value: myArray [4, 2, 3]

Let count = 0; / / used to record the number of traverses

Let result = myArray.filter (num = > {

Count++

Return num > 1

});

Console.log ("Array length is", myArray.length, "number of traverses is", count, "returned array is", result)

/ / print value: the length of the array is 4

/ / the number of traverses is 3

/ / the returned array is [4,2,3]

That's all for "traversing javaScript arrays and how to deal with sparse arrays". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report