简体   繁体   中英

How can I associate two values in Javascript or Jquery?

This seems like something that should be easy to do but I can't find any workable solutions. The basic idea is to have an array with codes associated to urls, and depending which url is parsed as a referrer, a code is inserted in localStorage.

var site = {
“example1.com” : "x10",
“example2.com” : "x11",
“example3.com” : "x12"
}

if (document.referrer.indexOf(sitevalue) != -1) {    

   if (localStorage) { 
   localStorage.setItem("code",sitekey);
}
}

So if the referrer is example1.com, then the code set in localStorage will "x10" and so on. I have read about possibly using the Underscore.js _Invert method, but I think there's gotta be a more obvious solution I am missing.

This is slightly inefficient, as it keeps processing after it finds the match, but it's very simple code:

Object.keys(site).forEach(function(key) {
    if (document.referrer.indexOf(key) > -1) {
        if (localStorage) { 
            localStorage.setItem("code", site[key]);
        }
    }
});

Update : Just noticed the Underscore tag. With Underscore, I might change the looping slightly, to do this instead:

_.forEach(_.pairs(site), function(pair) {
    if (document.referrer.indexOf(pair[0]) > -1) {
        if (localStorage) { 
            localStorage.setItem("code", pair[1]);
        }
    }
});

The only significant advantage is that this might work in older environments that don't support Object.keys , or Array.prototype.forEach .

If you want to get "x10" you can get it via site["example1.com"] , etc.

I'm sure about your specific requirement, but this would solve what I understood:

var site = {
    "example1.com" : "x10",
    "example2.com" : "x11",
    "example3.com" : "x12"
}

if (document.referrer.indexOf(sitevalue) != -1) {    
    if (localStorage) {
        var url = document.referrer.replace('www.','')
        var host = url.match(/:\/\/(.[^/]+)/)[1];
        localStorage.setItem("code", site[host]);
    }
}

Please note that I added some more code, to remove a possible leading www. as well as retireving the hostname from the provided URL via a regular expression.

If you want to leverage Underscore.js, the following standalone working example illustrates how to loop through the keys in your site object, and if one matches a referer string, then stash its corresponding code in localStorage.

<script src="http://underscorejs.org/underscore-min.js"></script>

<script>

    // this variable is just for testing
    var myReferer = "http://example2.com";

    var site = {
        "example1.com": "x10",
        "example2.com": "x11",
        "example3.com": "x12"
    };

    if (localStorage) {
        _.each(site, function (value, key) {
            // you'd replace "myReferer" with "document.referrer" here
            if (myReferer.indexOf(key) !== -1) {
                localStorage.setItem("code", value);
            }
        });
    }

    // this is also just for testing
    alert(localStorage["code"]);
</script>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM