Lit Aiy Lit Aiy - 3 months ago 31
Javascript Question

Get key from value with this node.js dictionary

I am using this node.js collection module.
http://www.collectionsjs.com/

I would like to get the key from value inside a dictionary. Here is my code;

"use strict";
var Dict = require("collections/dict");

var Data_type = new Dict(
{
"00": "DATA_NOT",
"01": "DATA_SENSOR",
"02": "DATA_SENSOR2",
"03": "DATA_SENSOR3",
});


Getting the value from key is easy.
Data_type.get("00");
will return
DATA_NOT
. However, I am encountering problem in getting the key from value. Preferably, I would like to do something like
Data_type.getKey("DATA_NOT");
and have it return
"00"
. Any other methods are welcomed.

Answer

will you have more than one same values in the dict ? another possible methods could use map() function in collectionjs

mapDict = [];
dict.map(function(v,k) { '
    if (v === "DATA_NOT") { 
    mapDict.add(v,k); 
    }
});

mapDict.get(0);
Comments