We help IT Professionals succeed at work.

Modify JS Code to check against up-to 4 keys instead of 1

894359
894359 asked
on
Hi,
I'm trying to figure out how to change the current code which originally only took a single key ("offerType") and used the "addToLookupTable" function to identify and add the associated value into the existing sTags1 array. What I want to do now is have the code check my values passed via the "addToLookupTable" function and if all the values in the  "addToLookupTable"  call exist in the current values being passed in the  _dtmPixelMapping function, then it is added to the sTags1 array. Please keep in mind the amount of arguments in the "addToLookupTable" function call can vary from 1-4  key values. All the keys, in the "addToLookupTable" function call,  regardless of amount, must match in order to be added the the sTags1 array.

*** EXAMPLES***
_dtmPixelMapping("fund now", "approved", "product A", "5.00") pushes  "value1"  & "value2" to sTags1 array  -- Reason:  line with value1 has all the values & line with value2 ("fund now", "", "product A") has all of its values (2 in this case) matching.
 
_dtmPixelMapping("fund now", "review", "product C", "25.00") pushes  "value7"  & "value10" to sTags1 array  -- Reason line with value7  has all its values & line with value10  has all its values (only 1 in this case)  matching ("product C")


var sTags1 = [
    '//mj.test.com/1',
    '//mj.test.com/2',
    '//mj.test.com/3'
]

function _dtmInsertUpdatedPixels(decision, currOffers) {
    var fundingType = sessionStorage.getItem("_dtmFundType");
    var fundingAmount = sessionStorage.getItem("_dtmFundAmount");

    if (currOffers != "") {
        var offers = currOffers.split("|");
        var offersLength = offers.length;

        for (var i = 0; i < offersLength; i++) {
            _dtmPixelMapping(fundingType, decision, offers[i], fundingAmount);
        }
    }
};

function _dtmPixelMapping(fundingType, decision, offerType, fundingAmount) {
    console.log(fundingType + " - " + decision + " - " + offerType + " - " + fundingAmount);
    offerType = offerType.toLowerCase();

    var lookupTable = {};
    var sTagsDecision = [];

    var addToLookupTable = function(key, value) {
        if (!Array.isArray(lookupTable[key])) {
            lookupTable[key] = new Array();
        }
        lookupTable[key].push(value);
    }


    addToLookupTable("fund now", "approved", "product A", "5.00", "value1");
    addToLookupTable("fund now", "", "product A", "", "value2");
    addToLookupTable("fund now", "approved", "product B", "5.00", "value3");
    addToLookupTable("fund later", "approved", "product A", "", "value4");
    addToLookupTable("fund later", "", "product A", "", "value5");
    addToLookupTable("fund now", "review", "product B", "", "value6");
    addToLookupTable("fund now", "", "product C", "", "value7");
    addToLookupTable("fund now", "review", "product A", "25.00", "value8");
    addToLookupTable("fund now", "", "product B", "35.00", "value9");
    addToLookupTable("", "", "product C", "", "value10");


    var relatedPixel = lookupTable[offerType];
    if (relatedPixel == null) {
        return;
    }

    for (var i = 0; i < relatedPixel.length; i++) {
        sTagsDecision.push(relatedPixel[i]);
    }
    _dtmInsertStaticTags(sTagsDecision);
};

//BELOW WAS FOR INTIAL TESTING
sessionStorage.setItem("_dtmFundAmount", "5.00");
sessionStorage.setItem("_dtmFundType", "fund now");
_dtmInsertUpdatedPixels("review", "product A|product B");

Open in new window

Thanks!
Comment
Watch Question

ste5anSenior Developer
CERTIFIED EXPERT

Commented:
Please craft a concise and complete example. And rephrase your questions.

As far as I understand your problem seems to be with the addToLookupTable() method. Thus explain what it should do with some examples in code and text.

For using a dynamic number of arguments look at using the arguments object. Another approach would be using a key array and a value. In the case of dynamic parameters, I prefer that the mandatory parameters are passed first.
Using a fixed number of keys is only a good idea, if your requirements really demand this (domain specific requirements).
Multitechnician
CERTIFIED EXPERT
Distinguished Expert 2019
Commented:
function _dtmPixelMapping(fundingType, decision, offerType, fundingAmount) {
    console.log(fundingType + " - " + decision + " - " + offerType + " - " + fundingAmount);
    offerType = offerType.toLowerCase();

    var lookupTable = {};
    var addToLookupTable = function(key1, key2, key3, key4,  value) {
        if (!Array.isArray(lookupTable[value])) {
            lookupTable[value] = new Array();
        }
        lookupTable[value].push([key1, key2, key3, key4]);
    }


    addToLookupTable("fund now", "approved", "product A", "5.00", "value1");
    addToLookupTable("fund now", "", "product A", "", "value2");
    addToLookupTable("fund now", "approved", "product B", "5.00", "value3");
    addToLookupTable("fund later", "approved", "product A", "", "value4");
    addToLookupTable("fund later", "", "product A", "", "value5");
    addToLookupTable("fund now", "review", "product B", "", "value6");
    addToLookupTable("fund now", "", "product C", "", "value7");
    addToLookupTable("fund now", "review", "product A", "25.00", "value8");
    addToLookupTable("fund now", "", "product B", "35.00", "value9");
    addToLookupTable("", "", "product C", "", "value10");


    var sTagsDecision = [];
    for (value in lookupTable) {
        if(lookupTable[value][0] == fundingType || lookupTable[value][1] == decision || lookupTable[value][2] == offerType || lookupTable[value][3] == fundingAmount) {
            sTagsDecision.push(value);
        }
    }
    _dtmInsertStaticTags(sTagsDecision);
};

Open in new window

Author

Commented:

Hi Leakim,

I know this is a hard problem to communicate so I included some test code with the expected results. Just to summarize, if all the values in the "addToLookUpTable" function call exist in the values passed in to the  _dtmPixelMapping function, then it should have the "value" passed. I created a test function with 3 expected outcomes. I hope this makes sense. Basically all the values in the   "addToLookUpTable" function must either all match or must be a subset of the values   passed in to the  _dtmPixelMapping function


function _dtmInsertUpdatedPixels(decision, currOffers) { 
    var fundingType = sessionStorage.getItem("_dtmFundType"); 
    var fundingAmount = sessionStorage.getItem("_dtmFundAmount"); 
 
    if (currOffers != "") { //check that there will always be an offer 
        var offers = currOffers.split("|"); 
        var offersLength = offers.length; 
 
        for (var i = 0; i < offersLength; i++) { 
            _dtmPixelMapping(fundingType, decision, offers[i], fundingAmount); 
        } 
    } 
}; 
 
function _dtmPixelMapping(fundingType, decision, offerType, fundingAmount) { 
    console.log(fundingType + " - " + decision + " - " + offerType + " - " + fundingAmount); 
    offerType = offerType.toLowerCase(); 
 
    var lookupTable = {}; 
    var addToLookupTable = function(key1, key2, key3, key4, value) { 
        if (!Array.isArray(lookupTable[value])) { 
            lookupTable[value] = new Array(); 
        } 
        lookupTable[value].push([key1, key2, key3, key4]); 
    } 
 
 
    addToLookupTable("fund now", "", "product a", "", "value1"); 
    addToLookupTable("fund now", "approved", "product a", "", "value2"); 
    addToLookupTable("fund now", "approved", "product a", "5.00", "value3"); 
    addToLookupTable("fund later", "review", "product b", "25.00", "value4"); 
 
 
 
    var sTagsDecision = []; 
    for (value in lookupTable) { 
        if (lookupTable[value][0] == fundingType || lookupTable[value][1] == decision || lookupTable[value][2] == offerType || lookupTable[value][3] == fundingAmount) { 
            sTagsDecision.push(value); 
        } 
    } 
	console.log("sTagsDecision : " + sTagsDecision); 
    //_dtmInsertStaticTags(sTagsDecision); 
}; 
 
 
function test(){ 
//Below should only return "value1" because only the line with value1 has only "fund now" & "product a" 
sessionStorage.setItem("_dtmFundAmount", "");  
sessionStorage.setItem("_dtmFundType", "fund now"); 
_dtmInsertUpdatedPixels("", "product a"); 
 
 
//Below should return "value1" because the line with value1 has "fund now" & "product a" 
//Below should return "value2" because the line with value2 has "fund now" & "product a" & "approved" 
sessionStorage.setItem("_dtmFundAmount", "");  
sessionStorage.setItem("_dtmFundType", "fund now"); 
_dtmInsertUpdatedPixels("approved", "product a"); 
 
 
//Below should return "value1" because the line with value1 has "fund now" & "product a" 
//Below should return "value2" because the line with value2 has "fund now" & "product a" & "approved" 
//Below should return "value3" because the line with value2 has "fund now" & "product a" & "approved" & "5.00" 
sessionStorage.setItem("_dtmFundAmount", "5.00");  
sessionStorage.setItem("_dtmFundType", "fund now"); 
_dtmInsertUpdatedPixels("approved", "product a"); 
}; 
 
test();


Thanks! 

Author

Commented:

I was able to work with what Leakim provided. I'll post my final code once completed. Thank you both.

Author

Commented:

Here is the final code I used which is a subset of a much larger file.

    function _dtmInsertUpdatedPixels(decision, currOffers) { 
 
        var fundingType = sessionStorage.getItem("_dtmFundType") !== null ? sessionStorage.getItem("_dtmFundType") : ''; 
        var fundingAmount = sessionStorage.getItem("_dtmFundAmount") !== null ? sessionStorage.getItem("_dtmFundAmount") : ''; 
 
        if (currOffers != "") { 
            var offers = currOffers.split("|"); 
            var offersLength = offers.length; 
 
            for (var i = 0; i < offersLength; i++) { 
                _dtmPixelMapping(fundingType, decision, offers[i], fundingAmount); 
            } 
        } else { 
            console.log("IN NO OFFERS ELSE"); 
            _dtmPixelMapping(fundingType, decision, "", fundingAmount); 
        } 
    }; 
 
    /************************************************************/ 
    // function _dtmPixelMapping 
    // 
    // Purpose: The following code enables the conditional invocation of marketing pixels. 
    //          If all(empty string is a wildcard) of the attributes from the  
    //          addToLookupTable call need to exist in the 4 current attributes passed in. 
    // 
    // Structure ==> addToLookupTable("[fundingType]","[decisionType]","[offerType]","[fundingAmount]","[pixelImgSrc]")           
    //  
    //fundingType (fund Now/Fund Later) ==> sessionStorage.getItem("_dtmFundType") 
    //decisionType (approve/review/decline) ==> passed explicitly 
    //offerType (premier checking, multiple possible) ==>  s.eVar15 
    //fundingAmount ($ 0+) ==> sessionStorage.getItem("_dtmFundAmount"); 
    /************************************************************/ 
 
    function _dtmPixelMapping(fundingType, decision, offerType, fundingAmount) { 
        console.log(fundingType + " - " + decision + " - " + offerType + " - " + fundingAmount); 
        offerType = offerType.toLowerCase(); 
 
        var lookupTable = {}; 
        var addToLookupTable = function(key1, key2, key3, key4, value) { 
            if (!Array.isArray(lookupTable[value])) { 
                lookupTable[value] = new Array(); 
            } 
 
            lookupTable[value].push([key1, key2, key3, key4]); 
        } 
 
        function invokeTiers(currTier, pixelTier) { 
 
            pixelTier = pixelTier.toLowerCase(); 
            pixelTier = pixelTier.replace(/ /g, ''); 
 
            switch (true) { 
                case (currTier == "tier1" && pixelTier == "tier1"): 
                    return true; 
                    break; 
                case (currTier == "tier2" && pixelTier == "tier2"): 
                    return true; 
                    break; 
                case (currTier == "tier3" && (pixelTier == "tier2" || pixelTier == "tier3")): 
                    return true; 
                    break; 
                default: 
                    return false; 
            } 
        }; 
 
        function getTier(amt) { 
 
            switch (true) { 
                case (amt < 5 && amt >= 0): 
                    return "tier1"; 
                    break; 
                case (amt < 25 && amt >= 5): 
                    return "tier2"; 
                    break; 
                case (amt >= 25): 
                    return "tier3"; 
                    break; 
                default: 
                    return "no tier"; 
            } 
        }; 
 
        var currTier = getTier(fundingAmount); 
 
        // Structure ==> addToLookupTable("[fundingType]","[decisionType]","[offerType]","[fundingAmount]","[pixelImgSrc]") 
        // BELOW PIXELS ARE INVOKED on decisionType = approve Only & product 
        addToLookupTable("", "approve", "premier checking", "", "//ad.doubleclick.net/ddm/activity/src=9443656;type=invmedia;cat=botw_003;dc_lat=;dc_rdid=;tag_for_child_directed_treatment=;tfua=;npa=;ord=1?"); 
        addToLookupTable("", "approve", "any deposit checking", "", "//ad.doubleclick.net/ddm/activity/src=9443656;type=invmedia;cat=botw_000;dc_lat=;dc_rdid=;tag_for_child_directed_treatment=;tfua=;npa=;ord=1?"); 
        addToLookupTable("", "approve", "choice interest checking", "", "//ad.doubleclick.net/ddm/activity/src=4587700;type=oao;cat=cic_oao1;u11=[URL];u12=[NEWVSRETURNING];dc_lat=;dc_rdid=;tag_for_child_directed_treatment=;tfua=;npa=;ord=1?"); 

 
 
        var sTagsDecision = []; 
 
        for (value in lookupTable) { 
 
            if ((lookupTable[value][0][0] == fundingType || lookupTable[value][0][0] === "") && (lookupTable[value][0][1] == decision || lookupTable[value][0][1] === "") && (lookupTable[value][0][2] == offerType || lookupTable[value][0][2] === "") && (invokeTiers(currTier, lookupTable[value][0][3]) || lookupTable[value][0][3] === "")) { 
 
                sTagsDecision.push(value); 
            } 
        } 
        console.log("\n################### START #####################\n#### Additional Pixels returned from addToLookupTable #### : \n fundingType : " + fundingType + "\n decision : " + decision + "\n offerType : " + offerType + "\n fundingAmount : " + fundingAmount + "\n", sTagsDecision); 
        console.log("###################  END  #####################\n"); 
        _dtmInsertStaticTags(sTagsDecision); 
    };
leakim971Multitechnician
CERTIFIED EXPERT
Distinguished Expert 2019

Commented:
thanks for sharing