Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fill a Javascript object literal with many static key/value pairs efficiently?

The typical way of creating a Javascript object is the following:

var map = new Object(); map[myKey1] = myObj1; map[myKey2] = myObj2; 

I need to create such a map where both keys and values are Strings. I have a large but static set of pairs to add to the map.

Is there any way to perform something like this in Javascript:

var map =  { { "aaa", "rrr" }, { "bbb", "ppp" } ... }; 

or do I have to perform something like this for each entry:

map["aaa"]="rrr"; map["bbb"]="ppp"; ... 

Basically, remaining Javascript code will loop over this map and extract values according to criterias known 'at runtime'. If there is a better data structure for this looping job, I am interested too. My objective is to minimize code.

like image 451
Jérôme Verstrynge Avatar asked Feb 05 '13 16:02

Jérôme Verstrynge


People also ask

Can an object key have multiple values JavaScript?

Summary. JavaScript doesn't support functions that return multiple values. However, you can wrap multiple values into an array or an object and return the array or the object. Use destructuring assignment syntax to unpack values from the array, or properties from objects.

How do you store key-value pairs in JavaScript?

Method 2: In this method we will use Map to store key => value in JavaScript. The map is a collection of elements where each element is stored as a key, value pair. Map objects can hold both objects and primitive values as either key or value.

Which data structure is used to maintain a key-value pair in JavaScript?

A Map is a collection of key🔑 — value pairs, similar to an object. It stores the key🔑 — value pairs in the insertion order. We can create a Map by passing an iterable object whose elements are in the form of key🔑 — value (Eg.

What are the two main objects when dealing with JavaScript?

JavaScript is designed on a simple object-based paradigm. An object is a collection of properties, and a property is an association between a name (or key) and a value. A property's value can be a function, in which case the property is known as a method.


2 Answers

In ES2015 a.k.a ES6 version of JavaScript, a new datatype called Map is introduced.

let map = new Map([["key1", "value1"], ["key2", "value2"]]); map.get("key1"); // => value1 

check this reference for more info.

like image 89
Nitin Avatar answered Sep 28 '22 01:09

Nitin


JavaScript's object literal syntax, which is typically used to instantiate objects (seriously, no one uses new Object or new Array), is as follows:

var obj = {     'key': 'value',     'another key': 'another value',      anUnquotedKey: 'more value!' }; 

For arrays it's:

var arr = [     'value',     'another value',     'even more values' ]; 

If you need objects within objects, that's fine too:

var obj = {     'subObject': {         'key': 'value'     },     'another object': {          'some key': 'some value',          'another key': 'another value',          'an array': [ 'this', 'is', 'ok', 'as', 'well' ]     } } 

This convenient method of being able to instantiate static data is what led to the JSON data format.

JSON is a little more picky, keys must be enclosed in double-quotes, as well as string values:

{"foo":"bar", "keyWithIntegerValue":123} 
like image 29
zzzzBov Avatar answered Sep 28 '22 01:09

zzzzBov