Julian
Julian

Reputation: 433

using electron-store with ip as key

Hy,

i try to use electron-store to persist values by ip as key. Using the code:

store.set('users.' + ip, 'foo')

So i want to achieve following object:

{users: [{"192.186.0.0": "foo"},{"192.186.0.1": "bar"}]}

but what i got is

"192": { "168": { "192": { "151": "Foo", "134": "Bar",.....

So is there a way to use ips?

Upvotes: 0

Views: 300

Answers (1)

obscure
obscure

Reputation: 12891

So what you want is essentially an array of objects, where the objects key is the ip address. In this case though I would rather recommend using an array of arrays because it makes working with the data a bit easier.

So first we create a new Map, which holds our users ip and name:

let map=new Map();
map.set('192.186.0.0', 'foo');
map.set('192.186.0.1', 'bar');

To store this Map using electron-store, we need to pack it into an array and populate the users key.

store.set('users', Array.from(map.entries(map)));

If we want to read out or modify values from the users key, we need to convert the array back to a map.

Let's look at an example - we want to modify 192.186.0.0 to say title instead of foo:

let map=new Map(store.get('users'));
map.set('192.186.0.0', 'title');
store.set('users', Array.from(map.entries(map)));

Upvotes: 1

Related Questions