Skip to content

Instantly share code, notes, and snippets.

@kurogelee
Created May 8, 2018 11:35
Show Gist options
  • Save kurogelee/e251cfe7be1fd9104f8d25e9f60d8519 to your computer and use it in GitHub Desktop.
Save kurogelee/e251cfe7be1fd9104f8d25e9f60d8519 to your computer and use it in GitHub Desktop.
TypeScriptでMap ⇔ Object変換 ref: https://qiita.com/kurogelee/items/8049c51a7c323ce0911e
import * as _ from "lodash";
function mapToObj<V>(map: Map<number, V>): _.NumericDictionary<V>;
function mapToObj<V>(map: Map<string, V>): _.Dictionary<V>;
function mapToObj<V>(map: Map<any, V>): _.Dictionary<V> {
return _.fromPairs([...map]);
}
function objToMap<V>(obj: _.NumericDictionary<V>): Map<number, V>;
function objToMap<V>(obj: _.Dictionary<V>): Map<string, V>;
function objToMap<V>(obj: _.Dictionary<V>): Map<any, V> {
return new Map(Object.entries(obj));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment