Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Associative arrays - ES6 [closed]

I know I can declare an associative "array" like:

var myData = {
    foo: 'val1',
    bar: 'val2',
    baz: 'val3'
};

What's standard practice in declaring associative arrays in ES6?

like image 508
panthro Avatar asked Jan 27 '16 17:01

panthro


People also ask

Is associative array supported in JavaScript?

JavaScript does not support associative arrays. You should use objects when you want the element names to be strings (text). You should use arrays when you want the element names to be numbers.

What is the difference between associative array and array?

The index data type for a simple array must be an integer value. The index type for an associative array can be one of a set of supported data types. The index values in a simple array must be a contiguous set of integer values. In an associative array the index values can be sparse.

How does an associative array work?

Associative arrays, also called maps or dictionaries, are an abstract data type that can hold data in (key, value) pairs. Associative arrays have two important properties. Every key can only appear once, just like every phone number can only appear once in a directory.

What is associative arrays in JS?

Associative arrays are basically objects in JavaScript where indexes are replaced by user defined keys. They do not have a length property like normal array and cannot be traversed using normal for loop.


1 Answers

Objects are associations of string keys and arbitrary values.

ES6 introduces maps, which are associations of arbitrary keys and arbitrary values.

var m = new Map([
  ['a', 'b'],
  [1, 2],
  [true, false]
]);
m.get('a'); // 'b'

There is no "standard practice", but maps can be considered when you want to associate values.

like image 91
Oriol Avatar answered Sep 25 '22 17:09

Oriol