端書き
- いつも通りの自分用備忘録
- 参照:)Extending TypeScript to serialise Map objects to JSON
結論
// Mapオブジェクトを作成する
let map = new Map()
let hoge = {
setting: {
label: 'aaa'
},
value: 'bbb',
isError: false,
}
let huga = {
setting: {
label: 'ccc'
},
value: 'ddd',
isError: true,
}
map.set('huga', huga)
map.set('hoge', hoge)
map.forEach((value,key) => {
console.log('key: ' + key + ' value:' + JSON.stringify(value))
})
// > "key: hoge value:{"setting":{"label":"aaa"},"value":"bbb","isError":false}"
// > "key: huga value:{"setting":{"label":"ccc"},"value":"ddd","isError":true}"
// JSON化する
let toJson = JSON.stringify(Array.from(map.entries()));
console.log('toJson: ' + toJson)
// > "toJson: [["hoge",{"setting":{"label":"aaa"},"value":"bbb","isError":false}],["huga",{"setting":{"label":"ccc"},"value":"ddd","isError":true}]]"
// 再度MAPオブジェクト化する
let toMap = new Map(JSON.parse(toJson));
toMap.forEach((value,key) => {
console.log('key: ' + key + ' value:' + JSON.stringify(value))
})
// > "key: hoge value:{"setting":{"label":"aaa"},"value":"bbb","isError":false}"
// > "key: huga value:{"setting":{"label":"ccc"},"value":"ddd","isError":true}"
戻った!