2

related (sort of) to this question. I have written a script that will loop through an object to search for a certain string in the referring URL. The object is as follows:

var searchProviders = {
  "google": "google.com",
  "bing": "bing.com",
  "msn": "search.msn",
  "yahoo": "yahoo.co",
  "mywebsearch": "mywebsearch.com",
  "aol": "search.aol.co",
  "baidu": "baidu.co",
  "yandex": "yandex.com"
};

The for..in loop I have used to loop through this is:

for (var mc_u20 in mc_searchProviders && mc_socialNetworks) { 
    if(!mc_searchProviders.hasOwnProperty(mc_u20)) {continue;}
    var mc_URL = mc_searchProviders[mc_u20];
    if (mc_refURL.search(mc_URL) != -1) {
        mc_trackerReport(mc_u20);
        return false;
    }

Now I have another object let's call it socialNetworks which has the following construct:

var socialNetworks = {"facebook" : "facebook.co" }

My question is, can I loop through both of these objects using just one function? the reason I ask is the variable mc_u20 you can see is passed back to the mc_trackerReport function and what I need is for the mc_u20 to either pass back a value from the searchProviders object or from the socialNetworks object. Is there a way that I can do this?

EDIT: Apologies as this wasn't explained properly. What I am trying to do is, search the referring URL for a string contained within either of the 2 objects. So for example I'm doing something like:

var mc_refURL = document.referrer +'';

And then searching mc_refURL for one of the keys in the object, e.g. "google.com", "bing.com" etc. 9this currently works (for just one object). The resulting key is then passed to another function. What I need to do is search through the second object too and return that value. Am I just overcomplicating things?

Community
  • 1
  • 1
zik
  • 3,035
  • 10
  • 41
  • 62
  • I don't quite understand what you are doing. Are you saying you want to process every property of `searchProviders` and every property of `socialNetworks` as if they were one big object, or are you trying to somehow relate properties of one to the other? – nnnnnn Jan 26 '12 at 10:52

3 Answers3

1

You could combine the two objects into one before your loop. There's several approaches here: How can I merge properties of two JavaScript objects dynamically?

var everything = searchProviders;
for (var attrname in socialNetworks) { everything[attrname] = socialNetworks[attrname]; }
for(var mc_u20 in everything) {
    // ...
}
Community
  • 1
  • 1
Lasar
  • 5,175
  • 4
  • 24
  • 22
  • Thanks for the answer and the link, wasn't sure that you could do this so thanks – zik Jan 26 '12 at 10:08
  • What is the `everything` variable for? You know this doesn't create a copy of `searchProviders`, it creates another reference to the same object and thus your code actually updates the `searchProviders` object? Also if the same property name exists in both objects the one that was in `searchProviders` will be overwritten with the value from `socialNetworks`. – nnnnnn Jan 26 '12 at 10:48
  • @nnnnnn i assume then that this solution doesn't answer my question. Is it easier for me to just merge both these objects and search through them that way (as per the link) – zik Jan 26 '12 at 10:51
  • I think it probably answers your question as long as the same property name isn't used in both objects. And as long as you don't mind that it actually changes your `searchProviders` object. – nnnnnn Jan 26 '12 at 10:54
  • Ah ok, the same property name isn't used in both objects so I think that this should be fine. When you say "changes your `searchProviders` object, do you mean it removes what's in there? – zik Jan 26 '12 at 11:01
  • What I'm saying is that the code as shown in this answer doesn't create a new object that has all the properties from both original objects, instead it adds the properties from `socialNetworks` to the existing `searchProviders` object. (The new variable `everything` doesn't create a copy.) – nnnnnn Jan 26 '12 at 11:18
1

If I understand your question correctly, you have a variable mc_refURL which contains some URL. You want to search through both searchProviders and socialNetworks to see if that URL exists as a value in either object, and if it does you want to call the mc_trackerReport() function with the property name that goes with that URL.

E.g., for mc_refURL === "yahoo.co" you want to call mc_trackerReport("yahoo"), and for mc_ref_URL === "facebook.co" you want to call mc_trackerReport("facebook").

You don't say what to do if the same URL appears in both objects, so I'll assume you want to use whichever is found first.

I wouldn't create a single merged object with all the properties, because that would lose information if the same property name appeared in both original objects with a different URL in each object such as in an example like a searchProvider item "google" : "google.co" and a socialNetworks item "google" : "plus.google.com".

Instead I'd suggest making an array that contains both objects. Loop through that array and at each iteration run your original loop. Something like this:

var urlLists = [
       mc_searchProviders,
       mc_socialNetworks
    ],
    i,
    mc_u20;

for (i = 0; i < urlLists.length; i++) {
   for (mc_u20 in urlLists[i]) { 
      if(!urlLists[i].hasOwnProperty(mc_u20))
         continue;
      if (mc_refURL.search(urlLists[i][mc_u20]) != -1) {
         mc_trackerReport(mc_u20);
         return false;
      }
   }
}

The array of objects approach is efficient, with no copying properties around or anything, and also if you later add another list of URLs, say programmingForums or something you simply add that to the end of the array.

nnnnnn
  • 147,572
  • 30
  • 200
  • 241
  • You sir are a legend. @nnnnnn you have explained this a lot better than I did. That is exactly what I am trying to do. Both objects are different so they won't contain the same URL. Gonna give this a try - thanks so much – zik Jan 26 '12 at 11:20
  • Cool. And note that (as in my "Google is a search engine _and_ a social networking site" example) at some point in the future you might update your objects such that the same key appears in both but with a different URL in each and if so my suggested solution will handle it. – nnnnnn Jan 26 '12 at 11:25
  • Yeah you're right. What I have decided I may do is prefix each key with some thing "search_facebook", "social_google" to try and stop these discrepancies. But your solution is elegant all the same. – zik Jan 26 '12 at 11:31
0
for (var i = 0; i < mc_searchProviders.length; i++) {
    var searchProvider = mc_searchProviders[i];
    var socialNetwork = mc_socialNetworks[i];
    if (socialNetwork != undefined) {
        // Code.
    }
}

Or am i horribly misunderstanding something?

Andreas Eriksson
  • 8,979
  • 8
  • 47
  • 65