How can you figure out the name of an object if you know what it's property is equal to?

I am working on a JavaScript program that helps you encrypt and decrypt messages. In order to make it very secure, I am planning on creating an object that contains the properties of id and text. Here is an example object:

var object1 = {
    id: "AE309JKL36784AJBCMDH"
    text: "∆∂˙˚∆ƒ˙ø©ˆ´¨¬˚å˙∂©¬˚"
}

Let's say I know that text is equal to ∆∂˙˚∆ƒ˙ø©ˆ´¨¬˚å˙∂©¬˚. How am I able to figure out that the name of the object that has this value for the property of text is object1?

Thank in advance.

2 answers

  • answered 2018-06-24 20:42 clickbait

    Object.keys(window).find((key) => {
        let obj = window[key];
        return obj && typeof obj === "object" && obj.text && obj.id && obj.text === "∆∂˙˚∆ƒ˙ø©ˆ´¨¬˚å˙∂©¬˚";
    });
    

  • answered 2018-06-24 20:53 Andriy

    It depends on the scope, where you declare your var, so I would rather use this (which will also work for window case):

    var object1 = {
      id:  "AE309JKL36784AJBCMDH",
      text: "∆∂˙˚∆ƒ˙ø©ˆ´¨¬˚å˙∂©¬˚"
    };
    
    console.log(Object.keys(this).find(k => this[k] && this[k].text && this[k].text === '∆∂˙˚∆ƒ˙ø©ˆ´¨¬˚å˙∂©¬˚'));
    

    @Dave Newton is absolutely right, this answer will not work for example in this case:

    (function(){
      var object1 = {
        id:  "AE309JKL36784AJBCMDH",
        text: "∆∂˙˚∆ƒ˙ø©ˆ´¨¬˚å˙∂©¬˚"
      };
    
      console.log(Object.keys(this).find(k => this[k] && this[k].text &&     this[k].text === '∆∂˙˚∆ƒ˙ø©ˆ´¨¬˚å˙∂©¬˚'));
    }())
    

    where this will be calling scope window