• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 33
  • Last Modified:

JavaScript Detect and combine N# of Arrays passed in to function?

I'm trying to figure out how to pass and combine an array when more than 1 is passed? The sample code below is the static version:
function insertStaticTags(){

        // Subroutine to remove tokens present in the static image src
        // and replace with actual data
        // - [RANDOMNUMBER]
        // - [URL]
        // - [NEWVSRETURNING]
        function detokenizeTags(imgSrc){
	         var rn = Math.random(),
            rnd = rn * 10000000000000;
            imgSrc = imgSrc.replace(/\[RANDOMNUMBER\]/g,rnd);
            imgSrc = imgSrc.replace(/\[URL\]/g,encodeURIComponent(location.href));
	         imgSrc = imgSrc.replace(/\[NEWVSRETURNING\]/g,_satellite.getVar("Customer Vs Anonymous"));
            return imgSrc; 
        };
        
        // Array of marketing pixel tags src provided with tokens
        var sTags = [
            '//insight.adsrvr.org/track/conv/?adv=fd0397w&ct=0:en272hj&fmt=3',
	         '//sp.analytics.yahoo.com/spp.pl?a=10000&.yp=10013756&ec=botw',
            '//googleads.g.doubleclick.net/pagead/viewthroughconversion/960526911/?guid=ON&script=0',
            '//mpp.vindicosuite.com/conv/v=5;m=1;t=24099;ts=[RANDOMNUMBER]',
            '//bm.adentifi.com/pixel/conv/ppt=1516;g=oao_confirmation;gid=8668;ord=[RANDOMNUMBER]']
   
        //routine to inject detokenized src values into container iframe
        var iframeTag = document.createElement("iframe");
        iframeTag.setAttribute("width", "1");
        iframeTag.setAttribute("height", "1");
        iframeTag.style.display = "none";
        iframeTag.style.border = "none";

        for(var i=0; i<sTags.length; i++) {
            var imgTag = document.createElement("img");
            imgTag.src = detokenizeTags(sTags[i]);
            iframeTag.appendChild(imgTag);
        }
        document.body.appendChild(iframeTag); 
    };

            // Invoke marketing tags
            insertStaticTags();

Open in new window

So In the above sample, the sTags array would need to be removed and passed in to the function but  I could possibly pass more than one array in (e.g. sTags2 and sTags3) to be processed at the same time if some condition dictates that. So I'll never know if I'm getting one or more arrays passed in. The naming of the arrays is up to me , if there is some way I can use that to my advantage?
0
894359
Asked:
894359
  • 2
  • 2
1 Solution
 
Jan LouwerensSoftware EngineerCommented:
Why not just have the function accept one array, but when calling the function, you could concatenate all of the desired arrays into one array.

For example:
insertStaticTags(sTags1.concat(sTags2, sTags3));

Open in new window

0
 
894359Author Commented:
I was thinking just use the arguments object/ array and then concatenate within the function?
0
 
Jan LouwerensSoftware EngineerCommented:
Yeah, that could work, too.

function insertStaticTags()
{
   var allSTags = [];
   for (var index = 0; index < arguments.length; index++)
      allSTags = allSTags.concat(arguments[index]);
   
   ...
}

Open in new window


var sTags1 = ['one', 'two', 'three'];
var sTags2 = ['four', 'five', 'six'];
var sTags3 = [];
var sTags4 = ['seven', 'eight'];

insertStaticTags(sTags1, sTags2, sTags3, sTags4);

Open in new window

0
 
894359Author Commented:
Thanks again Jan!
1
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now