Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8086019
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T18:19:18+00:00 2026-06-05T18:19:18+00:00

I have an object that has pairs of replacement values used for simple encoding

  • 0

I have an object that has pairs of replacement values used for simple encoding / decoding (not for security, just for a convenience; too complicated to explain it all here). It’s in the form

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

where ‘x’ is the value when encoded and ‘y’ is the decoded value.

Decoding is simple: I loop through the characters of the string, and look up the charAt(i) value in the object via brackets: obj[ str.charAt(i) ]. (I’m leaving out the check to see whether we need an uppercase or lowercase version (all key/values in the object are lowercase), but that’s simple enough.)

To encode, I of course have to look for the value in the object, rather than the property. Currently, I’m looping through the properties with a for ... in ... loop and checking the values against the charAt(i) value. 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 innately feel like there should be a more efficient way of doing this. (Of course having a reversed object, {y: x}, is an option. But not a good one.) Is this the best way, or is there a better? In essence, I’d love to be able to do var prop = obj[value] just like I can do var value = obj[prop].

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T18:19:19+00:00Added an answer on June 5, 2026 at 6:19 pm

    It’s more efficient to loop just once beforehand 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 of doing str.length * map.length, you’ll do map.length + str.length operations.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a big dictionary object that has several key value pairs (about 16),
Say I have a very simple java object that only has some getXXX and
I have two tables: object that has object_id column and avalues that have object_id
I have an object that has a multi-part key and I am struggling to
I have an object that has a child collection: class Person { public string
I have an object that has 3 separate Dictionaries. The value parameter for each
Lets say I have an object that has stringProp1, stringProp2. I wish to store
I have a domain object that has been auto generated for me by MyGeneration.
Suppose I have a customer object that has all of the standard customer properties
Using EF Code First I have an model object that has multiple properties that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.