最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Can I loop through 2 objects at the same time in JavaScript? - Stack Overflow

programmeradmin4浏览0评论

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",
  "bing": "bing",
  "msn": "search.msn",
  "yahoo": "yahoo.co",
  "mywebsearch": "mywebsearch",
  "aol": "search.aol.co",
  "baidu": "baidu.co",
  "yandex": "yandex"
};

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", "bing" 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 overplicating things?

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.",
  "bing": "bing.",
  "msn": "search.msn",
  "yahoo": "yahoo.co",
  "mywebsearch": "mywebsearch.",
  "aol": "search.aol.co",
  "baidu": "baidu.co",
  "yandex": "yandex."
};

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.", "bing." 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 overplicating things?

Share Improve this question edited May 23, 2017 at 12:08 CommunityBot 11 silver badge asked Jan 26, 2012 at 9:47 zikzik 3,09511 gold badges44 silver badges63 bronze badges 1
  • 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 Commented Jan 26, 2012 at 10:52
Add a ment  | 

3 Answers 3

Reset to default 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.".

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.

You could bine 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) {
    // ...
}
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?

发布评论

评论列表(0)

  1. 暂无评论