type a dictionary of arrays in javascript

To create a dictionary of arrays in JavaScript, you can use an object to hold the key-value pairs. Each value is an array that can hold multiple elements.

Here's an example:

index.tsx
let dictionary = {
  key1: ["value1", "value2", "value3"],
  key2: ["value4", "value5"],
  key3: []
};
103 chars
6 lines

In this example, the dictionary object has three keys (key1, key2, and key3) and each key maps to an array of values. The key1 array has three values, while key2 has two and key3 is empty.

To access a specific value in the dictionary, you can use bracket notation with the key name and the array index. For example:

index.tsx
console.log(dictionary.key1[0]); // outputs "value1"
53 chars
2 lines

You can also add new keys or update existing ones by assigning a new array to the key, like so:

index.tsx
dictionary.key4 = ["value6", "value7"];
dictionary.key1[0] = "new value";
74 chars
3 lines

These operations will add a new key key4 with an array of values, and update the first value in the key1 array to be "new value".

gistlibby LogSnag