9

The type definition for Map is as follows

interface MapConstructor {
    new <K, V>(): Map<K, V>;
    new <K, V>(iterable: IterableShim<[K, V]>): Map<K, V>;
    prototype: Map<any, any>;
}

I want to use the 2nd constructor new <K, V>(iterable: IterableShim<[K, V]>): Map<K, V>;

I have a type I've defined type ChangeMap = { [key: string]: Array<string> }; which is used as follows

let changeMap: ChangeMap = { A: ["Car", "Boat"], B: ["Plane", "Rocket"] };

I want to create a Map<number, ChangeMap> which I can do easily enough but I'm stuck as to how to structure the data I want to initialize my Map with

public changeOptions = new Map<number, ChangeMap>( ... what goes here ... ?)

Is this possible or do I need to initialize my Map without values and then call changeOptions.set(1, changeMap); to give it values?

--- EDIT #1

To answer @Paarth's question below, the number keys in the changeOptions Map are identifiers for other objects in the domain.

Assume I have a class of Option defined as follows

class Option {
    id: number;
    name: string;
}

The id of a given Option instance would be the key for the changeOptions Map. There are a limited predefined set of Options based on business rules for the domain. Assume for this question there are 3 Option instances with id values of 2, 10, 34, so I want those to be the keys of the changeOptions Map

2
  • 1
    Where do the numbers (first parameter in your set call) come from? Commented Sep 29, 2016 at 15:38
  • @Paarth The number keys for the changeOptions Map are IDs for other objects in the domain. Depending on that ID I want to use a different ChangeMap Commented Sep 29, 2016 at 16:37

2 Answers 2

7

The syntax is:

  let changeOptions = new Map<number, ChangeMap>([[2, changeMap], [10, anotherChangeMap], [34, thirdChangeMap]]);
Sign up to request clarification or add additional context in comments.

1 Comment

Confirmed - example here tsplay.dev/WkEyDN Thanks! 🙏
2

Where do the numbers come from? Are they just an order based on an array?

If so...

let changeMaps = ...

public changeOptions = new Map<number, ChangeMap>(changeMaps.map((v,i) => [i+1,v] as [number, ChangeMap]));

You don't actually need the <number, ChangeMap> anymore because the expression ends up as a Array<[number, ChangeMap]> thanks to the as [number, ChangeMap] but it doesn't hurt to have and it's good for documentation.

Breaking it down: If we want to have an iterable set of changes to pass into the map constructor, we need to store them in a collection. That's changeMaps up above, the ... is for you to fill it out how you wish. The naive form with one item is just [changeMap]. We then map that collection to a collection of tuples, including the indices. I used i+1 because it seemed like you wanted to start with the number 1. That's what gets passed into the map constructor.

1 Comment

I can't use auto-incrementing indexes as explained in EDIT #1 in my question

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.