T
T
Tenebrius2018-12-12 12:58:28
Node.js
Tenebrius, 2018-12-12 12:58:28

How to implement read with delete?

Good afternoon.
I have a data structure like:

Ключ1: значение1
Ключ2: значение2
...

It is necessary to walk through the structure, if there is a required key, return its value and remove the key-value pair. In other words, there should only be a one-time opportunity to return a certain value for a certain key.
What is the easiest way to implement this and, preferably, without a database?
UPD. Forgot the most important thing.
This structure will be accessed by many clients. Those. somewhere this data lies, the client sends a request and if it finds a match, it receives a value. At the same time, everyone who will connect after should not receive the data sent to the previous client.
Through the bot, people will request a promotional code, how to make sure that one promotional code does not end up with several people?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
@
@antoo, 2018-12-12
_

Not an expert in modern JS, but seems up to the task:

var hashMap = {'key1': 'value1', 'key2': 'value2'};

function get(key) {
    if (hashMap[key] == null) {
        return null;
    }
    var value = hashMap[key];
    delete hashMap[key];
    return value;
}

get('key1'); // "value1"
get('key1'); // null

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question