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 arrays in JavaScript

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

Share

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

In this issue, the editor will bring you about how to understand the array in JavaScript. The article is rich in content and analyzes and narrates it from a professional point of view. I hope you can get something after reading this article.

What is an array? Literally, an array is a combination of numbers, but it is not very accurate. To be exact, an array is a collection of data, that is, we put some data in a box and arrange it in order [1, 2, 3, 'hello', true, false]. This thing is an array that stores a collection of some data.

Data type classification

Number / string / boolean / undefined / null / object / function / array /...

Array is also one of the data types.

We simply divide all data types into two large categories: basic data types and complex data types.

Basic data type: number / string / boolean / undefined / null

Complex data types: object / function / array /...

Create an array

An array is a []

All kinds of data are stored in [], arranged in order.

Literally create an array

Create an array directly using []

/ / create an empty array var arr1 = []

/ / create an array with content var arr2 = [1,2,3]

Built-in constructor to create an array

Create an array using js's built-in constructor Array

/ / create an empty array var arr1 = new Array ()

/ / create an array of length 10 var arr2 = new Array (10)

/ / create an array with content var arr3 = new Array (1,2,3)

Length of the array

Length: meaning of length

Length represents the length of the array. The number of members in the array is the number of length.

/ / create an array var arr = [1, 2, 3]

Console.log (arr.length) / / 3

The index of the array

An index, also known as a subscript, refers to the position of a data in an array.

Note: in all languages, the index starts at 0.

The same is true in js, where the index of the array starts at 0

/ / create an array var arr = ['hello',' world']

In the above array, the 0th data is the string hello, and the 1st data is the string world

If you want to get the number in the array, use the array [index] to get the

Var arr = ['hello',' world']

Console.log (arr [0]) / / helloconsole.log (arr [1]) / / world

Differences in storage between data types (focus)

Now that we've distinguished basic data types from complex data types,

Then there must be some differences between them.

The biggest difference between them is in storage.

Our storage space is divided into two types of stack and heap

Stack: mainly stores the contents of basic data types

Heap: mainly stores the content of complex data types

Storage of basic data types in memory

Var num = 100, storage in memory

Store a data directly in the stack space

Storage of complex data types in memory

The storage of the following object

Var obj = {

Name: 'Jack'

Age: 18

Gender: 'male'}

Storage of complex data types

00001. Open up a storage space in the heap

00002. Store data in storage space

00003. Assign the address of the storage space to the variables in the stack

This is the difference in storage between data types.

Comparison between data types

The basic data type is the comparison between values.

Var num = 1var str ='1'

Console.log (num = = str) / / true

Complex data types are comparisons between addresses

Var obj = {name: 'Jack'} var obj2 = {name:' Jack'}

Console.log (obj = = obj2) / / false

Because we have created two objects, we will open up two storage spaces in the heap space to store data (two addresses).

Although the content of storage is the same, it is also two storage spaces and two addresses.

There is a comparison of addresses between complex data types, so the addresses of obj and obj2 are different.

So what we get is false.

Common methods of array

An array is a complex data type, and we can no longer operate like a basic data type when we operate on it.

For example, we want to change an array.

/ / create an array var arr = [1, 2, 3]

/ / We want to change the array to only 1 and 2arr = [1,2]

This must be unreasonable, because it is not before changing the array.

It is equivalent to giving an array to the variable arr.

It is equivalent to changing the address stored in arr, that is, changing the storage space instead of modifying it in the previous space.

So we need some methods to change the data in the storage space without changing the storage space.

Push of commonly used methods in arrays

Push is used to append an element to the end of an array

Var arr = [1,2,3]

/ / use the push method to append an element at the end of arr.push (4)

Console.log (arr) / / [1, 2, 3, 4]

Pop of commonly used methods in arrays

Pop is an element used to delete the end of an array

Var arr = [1,2,3]

/ / use the pop method to delete an element arr.pop () at the end

Console.log (arr) / / [1,2]

Unshift of commonly used methods in arrays

Unshift is to add an element to the front of the array

Var arr = [1,2,3]

/ / use the unshift method to add an element arr.unshift (4) to the front of the array

Console.log (arr) / / [4, 1, 2, 3]

Shift of commonly used methods in arrays

Shift is the first element of the deleted array

Var arr = [1,2,3]

/ / use the shift method to delete the first element of the array, arr.shift ()

Console.log (arr) / / [2,3]

Splice of commonly used methods in arrays

Splice is to intercept some of the contents of an array, according to the index of the array.

Syntax: splice (which index position to start, how many to intercept, new elements to replace) (the third parameter may not be written)

Var arr = [1,2,3,4,5]

/ / use the splice method to intercept the array arr.splice (1,2)

Console.log (arr) / / [1,4,5]

Arr.splice (1,2) indicates that 2 contents are intercepted from index 1.

The third parameter is not written, but there is no new content to replace the intercept position.

Var arr = [1,2,3,4,5]

/ / use the splice method to intercept the array arr.splice (1, 2,'I am new')

Console.log (arr) / / [1,'I am the New content', 4, 5]

Arr.splice (1, 2,'I am the new content') means to intercept 2 content from index 1.

Then fill in the position that has been intercepted with the third parameter.

Reverse of commonly used methods in arrays

Reverse is used to reverse the use of arrays

Var arr = [1,2,3]

/ use the reverse method to reverse the array arr.reverse ()

Console.log (arr) / / [3, 2, 1]

Sort of commonly used methods in arrays

Sort is used to sort arrays

Var arr = [2, 3, 1]

/ / use the sort method to sort the array arr.sort ()

Console.log (arr) / / [1,2,3]

This is just a basic and simple use.

Concat of commonly used methods in arrays

Concat is to splice multiple arrays.

Unlike the previous method, concat does not change the original array, but returns a new array.

Var arr = [1,2,3]

/ / use the concat method to concatenate the array var newArr = arr.concat ([4,5,6])

Console.log (arr) / / [1,2,3] console.log (newArr) / / [1,2,3,4,5,6]

Note: the concat method does not change the original array

Join of commonly used methods in arrays

Join links every item in the array into a string.

You can define the content join that links between each item (with what content to link)

Does not change the original array, but returns the linked string

Var arr = [1,2,3]

/ / use join to link the array var str = arr.join ('-')

Console.log (arr) / / [1,2,3] console.log (str) / / 1-2-3

Note: the join method does not change the original array, but returns the linked string

For and for in cycle

Because the index of the array can get the contents of the array

The index of the array is arranged in 0 ~ n order.

We can use the for loop to loop the array, because the for loop can also be set to increase in the order of 0 ~ n.

We call this behavior traversal.

Var arr = [1,2,3,4,5]

/ / use for to loop through the array for (var I = 0; I

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