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 use JavaScript array

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

Share

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

This article mainly introduces how to use the JavaScript array, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor take you to understand it.

Array introduction

Array (Array)-an array is also an object

It is similar to our ordinary object function, but also used to store some values.

The difference is that ordinary objects use strings as attribute values, while arrays use numbers as index operation elements.

Index: integer starting at 0

The storage performance of arrays is better than that of ordinary objects. in development, we often use arrays to store some data.

Create an array:

Var arr = new Array ()

When you check an array with typeof, it returns object

Add elements to the array

Syntax: array [index] = value

Read elements in an array

Syntax: array [index]

If you read an index that does not exist, it will not report an error but return undefined

Get the length of the array

You can use the length attribute to get the length of the array (number of elements)

Syntax: array .length

For consecutive arrays, you can use length to get the length of the array (the number of elements)

For discontiguous arrays, using length will get the largest index of the array + 1

Try not to create discontiguous arrays.

Modify length

If the modified length is less than the original length, the extra part will be empty.

If the modified length is less than the original length, the extra elements will be deleted

Add an element to the last position of the array

Syntax: array [array .length] = value

Arr [arr.length] = 70 [arr.length] = 80 [arr.length] = 90; array literals

Create an array using array literals

Grammar: []

Var arr = []

When you create an array using literals, you can specify the elements in the array at the time of creation

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

When you use the builder to create an array, you can also add elements at the same time. The elements to be added are passed as parameters of the constructor, and the elements are used and separated.

Var arr = new Array (1, 2, 3, 4, 5)

Note:

Create an element 10 in an array with []

Var arr = [10]

When you use the builder to create an array as a parameter, you create an empty array of length 10

Var arr = new Array (10); console.log (arr); console.log ("arr.length=" + arr.length)

There can be any data type in the array

Var arr = ["Sun WuKong", 1, true, null, undefined]; console.log (arr)

Can be an object.

Var arr = [{name: "Sun WuKong"}, {name: "Zhu Bajie"}, {name: "Sha Wujing"}]; console.log (arr [0] .name)

Can be a function.

Var arr = [function () {alert (1)}, function () {alert (2)}]

Transfer the function through arr [0] ()

Two-dimensional array

Create:

Use []

Var arr = [[1mage2, 3], [4, 5, 5, 6], [7, 8, 9]]; / / 2-D array with 3 rows and 3 columns

Use new Array

Var a = new Array (new Array (10meme 20rem 30), new Array (11pr 22rem 33), new Array (45meme 56je 67))

The access array name of the element [row subscript] [column subscript]

(1) transpose of two-dimensional array:

Var a = [['axiajukui'], ['dongypaijiezhongyuanf'], [' gongyuzhongyuzhongyui'], ['iLiangzhongyangjiaoyui']] var str =''for (var iLiquidi)

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