简体   繁体   中英

How to make associative array with number as string in Javascript

I have a code :

var index = 100;

var arr =[];

arr[index.toString()] = "Hello"

The result : index still known as integer not a string. Anyone can explain what's wrong with my code?

You have to declare associative arrays using {} , which creates a new object , because in JavaScript, arrays always use numbered indexes.

You need to declare an object: var arr={};

  • arrays use numbered indexes .
  • objects use named indexes .

 var index = 100; var arr ={}; arr[index.toString()] = "Hello"; console.log(arr); 

How to make associative array with number as string in Javascript

JavaScript doesn't have associative arrays in the sense that term is frequently used. It has objects, and as of ES2015 (aka "ES6"), it has Map s.

The result : index still known as integer not a string. Anyone can explain what's wrong with my code?

The index variable 's value is still a number, yes, because you haven't done anything to change it. But the index in the array is a string (and would be even if you didn't use .toString() ), because standard arrays aren't really arrays at all 1 , they're objects with special handling of a class of properties (ones whose names are strings that fit the spec's definition of an array index), a special length property, and that use Array.prototype as their prototype.

Here's proof that array indexes are strings:

 var a = []; a[0] = "zero"; for (var name in a) { console.log("name == " + name + ", typeof name == " + typeof name); } 

That said, you don't want to use an array when you want a generic object or map.

Here's using a generic object for name/value mappings:

 var o = Object.create(null); var name = "answer"; o[name] = 42; console.log(o[name]); // 42 

The property names in objects are strings or (as of ES2015) Symbols. I used Object.create(null) to create the object so it wouldn't have Object.prototype as its prototype, since that gives us properties ( toString , valueOf , etc.) that we don't want if we're using the object as a map.

Here's using a Map :

 var m = new Map(); var name = "answer"; m.set(name, 42); console.log(m.get(name)); // 42 

The main advantages Map s have over objects are:

  • Their keys can be anything, not just strings or Symbols
  • They're iterable , so you can use for-of to loop through the mappings they contain
  • Map s have a size property telling you how many entries they have
  • Map s guarantee that iteration of their entries is performed in the order the entries were added to the map

With ES6, you could use a Map , which holds any type as key.

 var map = new Map; map.set(100, "Hello"); map.set('100', "Foo"); console.log(map.get(100)); // 'Hello' console.log(map.get('100')); // 'Foo' console.log([...map]); 

JavaScript does not support arrays with named indexes , in JavaScript, arrays always use numbered indexes.

If you use a named index, JavaScript will redefine the array to a standard object. After that, all array methods and properties will produce incorrect results.

As you can see in the following example:

 var person = []; person["firstName"] = "John"; person["lastName"] = "Doe"; person["age"] = 46; var x = person.length; // person.length will return 0 console.log(x); var y = person[0]; // person[0] will return undefined console.log(y); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM