multi-index-map
v1.1.0
Published
a map that can set multiple indexes on a given list of objects
Downloads
24
Maintainers
Readme
MultiIndexMap
Browser Support
| | | | | | --- | --- | --- | --- | --- | --- | Latest ✔ | Latest ✔ | Latest ✔ | Latest ✔ | Latest ✔ | 11 ✔ |
Installing
Using npm:
$ npm install multi-index-map
Using yarn:
$ yarn add multi-index-map
Usage :
const contacts = [
{ id: 'ddd-33sdfia', email: '[email protected]', number: '8787898989', state: 'India', ... },
{ id: 'eee-swwwadd', email: '[email protected]', number: '676776676', state: 'US', ... },
{ id: '222-33sdssf', email: '[email protected]', number: '898976789', state: 'India', ...}
...
];
// id, email, number are unique properties in the among the above contacts array
// we can form a map based on these unique properties
const contactsMap = new MultiIndexMap(['id', 'email', 'number'], contacts);
// now you can use one of the above three properties to get access to the objects
console.log(contactsMap.get('id', 'eee-swwwadd'));
// { id: 'ddd-33sdfia', email: '[email protected]', number: '8787898989' ... }
console.log(contactsMap.get('email', 'unique3email.com'))
// { id: '222-33sdssf', email: '[email protected]', number: '898976789', state: 'India', ...}
console.log(contactsMap.get('number', '676776676'))
// { id: 'eee-swwwadd', email: '[email protected]', number: '676776676'..},
// provides methods which Map has like has, set, delete methods
console.log(contactsMap.has('email', 'unique3email.com')) // true
console.log(contactsMap.delete('id', '222-33sdssf')) // removes item with id : 222-33sdssf
const newContact = { id: '4443-2sss8',email: '[email protected]', number: '999090090', state: 'India', ...};
contactsMap.set(newContact); // will add the item to the map
Time and Space Complexities
where m: no of indexes, n: no of items
- only properties are indexed and items are not duplicated: m-1 indextables n items each = O((m - 1) * n) 1 main table with n items = O(n)
- operations
| operation | complexity | |--------------------|--------| | new MultiIndex(..) | O(m*n) | | get | O(1) | | set | O(m) | | has | O(1) | | delete | O(m) |