Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use numbers to index arrays in JavaScript

Shulou Source: shulou.com Published: 2022-06-01 09:30:22 10月01日 Update

This article mainly explains "how to use numbers to index arrays in JavaScript". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to use numbers to index arrays in JavaScript.

Many programming languages allow names to be used as indexes on arrays.

An array that uses a name as an index is called an associative array (or hash).

JavaScript does not support indexing arrays with names, only numeric indexes are allowed.

Example

Var person = []

Person [0] = "John"

Person [1] = "Doe"

Person [2] = 46

Var x = person.length; / / person.length returns 3

Var y = person [0]; / / person [0] returns "John"

In JavaScript, objects use the name as the index.

If you use the name as the index, JavaScript redefines the array as a standard object when accessing the array.

After doing this, the methods and properties of the array can no longer be used, otherwise an error will occur:

Example

Var person = []

Person ["firstName"] = "John"

Person ["lastName"] = "Doe"

Person ["age"] = 46

Var x = person.length; / / person.length returns 0

Var y = person [0]; / / person [0] returns undefined

At this point, I believe you have a deeper understanding of "how to use numbers to index arrays in JavaScript". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Tags: Array index name number content instance object method learning practical deeper interest practical attribute simple operation more friends standard program Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno macOS MariaDB Shulou Technology OPPO Reno Apple