Json - Take key from a value

Asked

Viewed 378 times

0

I own a json:

{
    "pendente": 0,
    "cancelada": 1,
    "remitida": 2
}

I matter that way in my file.js:

const enumJson = require("./dir/enum.json");

I wonder, how do I get the key from a value. For example, I have a value 2, and want as a result the data: "remitted".

Taking advantage, I do not know if it is a good practice to do so, I am open to suggestions.

  • 1

    It would not be better to use an array and access the indexers from it?

2 answers

1

A simpler and more direct solution is possible:

Getting your original data:

data = {
  "pendente": 0,
  "cancelada": 1,
  "remitida": 2
};

This expression will return the string "remitida":

Object.entries(data).find((key, value)=> value == 2 )[0]

Explanation:

Object.entries() is a mixture of Object.keys() and Object.values(), it will remove an array of arrays based on this dictionary. The result will be the following:

[
  ["pendente", 0],
  ["cancelada", 1],
  ["remitida", 2]
]

Arrays has the method find() which returns the first element whose filter function returns true (or values equivalent to true, nonempty strings and non-zero numbers). In this case find will return:

["remitida", 2]

Oh, yes, the "filter function" there is (key, value)=> value == 2, an anonymous function that will receive the two items of each child array in the variables key and value, what they originally are. Then just test if the value is what is expected with value == 2.

Like I said before the find() will return an array (IN THIS CASE), and what we want is the first item of this (which holds the key), so a [0] after the find().

  • Dude. Very interesting your answer. Thank you very much, I will use here.

1


The object of objects is not to search for keys by values, but values by keys. If you try to search for keys by values, you will encounter two problems:

  • Unnecessarily creating code for a simple problem.
  • If there are keys with equal values, how will you know which key is correct?

In this case, the correct one would be to create an array to store these strings. This way you can get them through an index (integer).

const array = ["pendente", "cancelada", "remitida"];
console.log(array[2]);

But if you really need it to be a JSON object, then you must reverse the positions by placing the keys as values and the values as keys. See the example below:

const object = {
    0: "pendente",
    1: "cancelada",
    2: "remitida"
};
console.log(object[2]);

But if you still want to search for keys by values in your JSON, there is a solution. Just go through each key and check its values with a repeat loop. However, this way you may come across the second problem I mentioned up there.

const object = {
    "pendente": 0,
    "cancelada": 1,
    "remitida": 2
}

const valor = 2;
let chave;

for (key in object) {
    if (object[key] == valor) {
        chave = key;
    }
}

console.log(chave);

  • Man, very good. Thanks for the answer.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.