Javascript: quickly find the value in an object (for example, we can with properties)

I have an object that has pairs of substitute values ​​used for simple encoding / decoding (not for security, just for convenience, it's all too difficult to explain all this here). He's in shape

var obj = {x: y,
           x: y,
           ...
          };

where "x" is the encoding value, and "y" is the decoded value.

Decoding is simple: I scroll line characters and view the value of charAt(i)the object through the brackets obj[ str.charAt(i) ]. (I can’t check if we need a uppercase or lowercase version (all keys / values ​​in the object are lowercase), but this is quite simple.)

To encode, of course, I have to look up the value in the object, not the property. I am currently looping through properties with a loop for ... in ...and checking values ​​by value charAt(i). My current code is:

var i, j,
    output = '',
    str = 'Hello World!',
    obj = {'s':'d',
           'm':'e',
           'e':'h',
           'x':'l',
           'z':'o',
           'i':'r',
           'a':'w',
           'o':'!',
           '-':' '};
for (i = 0; i < str.length; i++) {
    for (j in obj) {
        if (Object.prototype.hasOwnProperty.call(obj, j) &&
            Object.prototype.propertyIsEnumerable.call(obj, j)) {
            if (obj[j] === str.charAt(i)) {
                output += j;
                break;
            } else if (obj[j].toUpperCase() === str.charAt(i)) {
                output += j.toUpperCase();
                break;
            }
        }
    }
}
alert(output);

I naturally feel that there must be a more efficient way to do this. (Of course, having a return object, {y: x}, is an option, but not a good one). Is this the best way or better? In essence, I would like to do var prop = obj[value]how I can do var value = obj[prop].

+5
source share
4 answers

A more efficient loop only once in advance to create a reverse map:

var str = "Hello World!",
    output = '',
    map = {
      "s":"d", "m":"e",
      "e":"h", "x":"l",
      "z":"o", "i":"r",
      "a":"w", "o":"!",
      "-":" "
    },
    reverseMap = {}

for (j in map){
  if (!Object.prototype.hasOwnProperty.call(map, j)) continue
  reverseMap[map[j]] = j
}

output = str.replace(/./g, function(c){
  return reverseMap[c] || reverseMap[c.toLowerCase()].toUpperCase()
})

console.log(output)

Instead, str.length * map.lengthyou perform operations map.length + str.length.

+5
source

, hasOwnProperty ..

var str= 'Hello World!',
obj={
    's':'d',
    'm':'e',
    'e':'h',
    'x':'l',
    'z':'o',
    'i':'r',
    'a':'w',
    'o':'!',
    '-':' '
}
str= str.replace(/./g, function(w){
    for(var p in obj){
        if(obj[p]=== w) return p;
        if(obj[p]=== w.toLowerCase()) return p.toUpperCase();
    };
    return w;
});

: (String) Emxxz-Azixso

+3

You can create the reverse version of the conversion programmatically (rather than manually) and use it instead.

var rev = {}
for (key in obj)
    rev[obj[key]] = key
+2
source

If you are looking for array keys, check here.

https://raw.github.com/kvz/phpjs/master/functions/array/array_keys.js

function array_keys (input, search_value, argStrict) {
    var search = typeof search_value !== 'undefined', tmp_arr = [], strict = !!argStrict, include = true, key = '';

    if (input && typeof input === 'object' && input.change_key_case) {
        return input.keys(search_value, argStrict);
    }

    for (key in input) {
        if (input.hasOwnProperty(key)) {
            include = true;
            if (search) {
                if (strict && input[key] !== search_value) include = false;
                else if (input[key] != search_value) include = false;
            }
            if (include) tmp_arr[tmp_arr.length] = key;
        }
    }

    return tmp_arr;
}
0
source

All Articles