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?
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With