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

How to understand JavaScript array

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

Share

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

This article mainly explains "how to understand the JavaScript array", the content of the article is simple and clear, easy to learn and understand, now please follow the editor's ideas slowly in depth, together to study and learn "how to understand the JavaScript array"!

Catalogue

First, the role of the array:

2. the definition of the array:

1. Create an array through a constructor

two。 Create an array literally

3. Array elements

4. Array length

5. Array index (subscript)

VI. problems to be paid attention to in arrays

1. The data stored in the array can be different

two。 The length of the array can be changed.

3. Anyway,

Traversing the array

1. Positive order ergodic

two。 Reverse traversal

8. Common cases in the array

1. Find the sum of all the elements in the array

two。 Find the average of the array

3. Find the maximum and minimum values of the array

4. Bubbling sort

Summary

Array: an ordered set of data

First, the role of the array:

You can store multiple data at once

Second, the definition of array: 1. Create an array through a constructor

Syntax:

Var array name = new Array ()

Var array=new Array (); / / defines an array

/ / define an array var array=new Array () by constructor; / / an empty array with no data

If the data in the array is output directly, then the data in the array can be displayed directly; if there is no data, the data cannot be seen.

Var array name = new Array (length)

If there is no data in the array, but there is a length, each value in the array is undefined.

When creating an array with a constructor, if it is the length of the Array (a number) → exponential group (that is, the number of array elements); if it is Array (multiple values) → means that there is data in the array, and the length of the array is the number of these data.

two。 Create an array literally

Syntax:

Var array name = []; / / empty array

Var arr= []; console.log (arr); / / Array [0]

Summary:

Whether it is a constructor or an array defined literally, if there is a length, the default is undefined.

3. Array elements

Every data stored in an array can be called an element of an array.

For example, if there are three arrays stored in the array, then the array has three elements.

4. Array length

The length of the array is the number of elements

For example, if there are three elements, say that the length of the array is 3.

Var arr1=new Array (); / / creates an empty array var arrr2=new Array (5) by constructor; / / creates an array of 5 in length with five values, all of which are undefinedvar arr3=new Array (10, 10, 20, 30, 40, 50); / / the constructor creates an array five, array index (subscript) with length 5, each value meaningful.

The index of the array, or the subscript of the array. Is used to store or access data in my array, starting at 0 and ending at length-1

How to set the value of a location in an array:

Array name [subscript] = value

For example: arr [3] = 100

How to get the value of a location in an array:

Var result= array name [subscript]

Console.log (result)

/ / get the fourth value in the array, var arr = new Array; console.log (arr [4]); / / change the value of the subscript 3 to 1000arr [3] = 1000 Legend / get the length of the array in literal terms var arr1= [10 camera 20, 30, 40, 50]; console.log (arr.length); / / 6

The relationship between array length and index:

Length-1 = index

Sixth, the array attention question 1. The data stored in the array can be different arr= [10, "", true,null,undefined,new Object ()]; 2. The length of the array is changeable var arr = []; / / set the value of the elements in the array arr [0] = 10; arr [1] = 20; console.log (arr.length); / / 2 / / get the value of the element console.log (arr [2]) by index; / / undefined3. In short, var arr1 = new Array (); / / empty array var arr2 = new Array (5); / / array of length 5, the value of each array is undefinedvar arr3 = new Array (1,2,3,4,5); / / array of length 5, each value is meaningful var arr4 = []; / empty array var arr5 = [1,2,3]; / array of length 3 var arr6 = ["red", "blue", 1, true] / / an array with a length of 4 and different element data types var arr7 = []; / / declare an empty array and add the value arr7 [0] = 10 to Arr7 [1] = 30; 7. Traverse the array 1. Traversing var arr= in positive order [10 var 20]; / / I means that the subscript of the array / / the subscript of the last number equals to the length of the array minus one for (Arr [I]);} the common case in the array 1. Find the sum of all the elements in the array var arr = [10,20,30,40,100]; / / to store the summation var sum = 0; for (var I = 0; I)

< arr.length; i++) { sum += arr[i]; } console.log(sum);//2002.求数组的平均数var arr = [10, 20, 30, 40, 100];// 用来存储相加后的总和var sum = 0;for (var i = 0; i < arr.length; i++) { sum += arr[i];}console.log(sum / arr.length); //403.求数组的最大值和最小值var arr = [10, 20, 30, 40, 100];// 假设数组中第一个数是最大的,并将它赋值给一个变量var max = arr[0];// 假设数组中第一个数是最小的,并将它赋值给一个变量var min = arr[0];for (var i = 0; i < arr.length; i++) { //用数组中的值和max进行比较,如果比max大,则将那个数赋值给max if (max < arr[i]) { max = arr[i]; } //用数组中的值和min进行比较,如果比min小,则将那个数赋值给min if (min >

Arr [I]) {min = arr [I];}} console.log (max); / / 100console.log (min); / / 104. Bubble sort var arr = [10,30,20,65,40,8100]; / / sort for from small to large (var I = 0; I)

< arr.length; i++) { for (var j = 0; j < arr.length - i; j++) { if (arr[j] >

Arr [j + 1]) {var temp = arr [j]; arr [j] = arr [j + 1]; arr [j + 1] = temp;} console.log (arr); / / sort for from largest to smallest (var I = 0; I < arr.length; I +) {for (var j = 0; j < arr.length-I) If (arr [j] < arr [j + 1]) {var temp = arr [j]; arr [j] = arr [j + 1]; arr [j + 1] = temp;} console.log (arr)

Thank you for your reading, the above is the content of "how to understand the JavaScript array", after the study of this article, I believe you have a deeper understanding of how to understand the JavaScript array, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

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