Use object as a key for Map
npm install @lexriver/json-mapnpm install @lexriver/json-map
typescript
import { JsonMap } from '@lexriver/json-map'
`
Usage
`typescript
// create new map with key type {name:string, age:number} and number as a value
let valueByPerson = new JsonMap<{name:string, age:number}, number>()
valueByPerson.set({name:'John', age: 10}, 100)
valueByPerson.set({name:'Smith', age: 20}, 200)
valueByPerson.get({name:'John', age: 10}) // 100
valueByPerson.get({name:'John', age: 20}) // undefined
valueByPerson.get({name:'Smith', age: 20}) // 200
valueByPerson.get({name:'smith', age: 20}) // undefined
`
Methods
$3
`typescript
let myMap = new JsonMap()
`
K and V could be of any type including Object
$3
`typescript
const myMap = new JsonMap`
$3
Get value by key. Returns undefined if key not found.
`typescript
myMap.get({name:'John', age:10})
`
$3
Create or update value for key.
`typescript
myMap.set({name:'John', age:10}, 100)
`
$3
Check if map has a key.
`typescript
myMap.has({name:'John', age:10})
`
$3
Delete one item by key.
`typescript
myMap.delete({name:'John', age:10})
`
$3
Remove all items from map.
`typescript
myMap.clear()
`
$3
Get count of items in map.
`typescript
myMap.size
`
$3
Get all keys in map.
`typescript
for(const key of myMap.keys()){
//...
}
`
$3
Get all values in map.
`typescript
for(const value of myMap.values()){
// ...
}
`
$3
Get map as array.
`typescript
myMap.toArray()
`
$3
Fill map with keys and values from array.
`typescript
myMap.initFromArray(mySavedArray)
`
$3
Get map as json string.
`typescript
myMap.toJsonString(true)
`
$3
Fill map with keys and values from json string.
`typescript
myMap.initFromJsonString(mySavedJsonString)
``