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

What is the difference between JavaScript associative arrays, arrays, and objects?

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

Share

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

This article mainly explains "what is the difference between JavaScript associative array, array and object". The content of the article is simple and clear, and it is easy to learn and understand. Please follow Xiaobian's train of thought to study and learn "what is the difference between JavaScript associative array, array and object".

Associative array

Many programming elements support arrays of named indexes.

An array with a named index is called an associative array (or hash).

JavaScript does not support arrays with named indexes.

In JavaScript, arrays can only use numeric indexes.

Example

Var person = []

Person [0] = "Bill"

Person [1] = "Gates"

Person [2] = 62

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

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

Warning!

If you use a named index, JavaScript redefines the array as a standard object.

After that, all the methods and properties of the array will produce incorrect results.

Example:

Var person = []

Person ["firstName"] = "Bill"

Person ["lastName"] = "Gates"

Person ["age"] = 62

Var x = person.length; / / person.length will return 0

Var y = person [0]; / / person [0] will return undefined

The difference between arrays and objects

In JavaScript, arrays use numeric indexes.

In JavaScript, objects use named indexes.

Arrays are special types of objects with numeric indexes.

When to use arrays and when to use objects?

JavaScript does not support associative arrays

If you want the element name to be a string (text), you should use an object.

If you want the element to be named as a number, you should use an array.

Avoid new Array ()

There is no need to use JavaScript's built-in array constructor new Array ().

Please use [] instead!

The following two different statements create a new empty array named points:

Var points = new Array (); / / poor

Var points = []; / / excellent

The following two different statements create a new array of six numbers:

Var points = new Array (40,100,1,5,25,10); / / difference

Var points = [40,100,1,5,25,10]; / / excellent

The new keyword only complicates the code. It can also produce some unpredictable results:

Var points = new Array (40,100); / / create an array of two elements (40 and 100)

What happens if you delete one of the elements?

Var points = new Array (40); / / create an array of 40 undefined elements!

How to identify arrays

The common question is: how do I know if a variable is an array?

The problem is that the JavaScript operator typeof returns "object":

Var fruits = ["Banana", "Orange", "Apple", "Mango"]

Typeof fruits; / / returns object

The typeof operator returns "object" because the JavaScript array belongs to the object.

Solution 1:

To solve this problem, ECMAScript 5 defines a new method, Array.isArray ():

Array.isArray (fruits); / / returns true

The problem with this solution is that ECMAScript 5 does not support older browsers.

Solution 2:

Create your own isArray () function to solve this problem:

Function isArray (x) {

Return x.constructor.toString (). IndexOf ("Array") >-1

}

If the argument is an array, the above function always returns true.

Or, more accurately, return true if the object prototype contains the word "Array".

Solution 3:

If the object is created by a given constructor, the instanceof operator returns true:

Var fruits = ["Banana", "Orange", "Apple", "Mango"]

Fruits instanceof Array / / returns true

Thank you for reading, the above is the content of "what is the difference between JavaScript associative array, array and object". After the study of this article, I believe you have a deeper understanding of what is the difference between JavaScript associative array, array and object. 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