Advertisement
NonplayerCharacter

IIFE | m. | List all values of a property in data layer

May 8th, 2017
321
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. (function(property) {
  2.     for (var index = 0; index < dataLayer.length; index++) {
  3.         if (dataLayer[index].ajaxLists != undefined) {
  4.             console.log("It's dataLayer[" + index + "]");
  5.             var theList = dataLayer[index].ajaxLists.vehicles;
  6.             for (var i = 0; i < theList.length; i++) {
  7.                 var theVehicle = theList[i];
  8.                 // console.log( "Make: " + theVehicle.make );
  9.                 console.log(theVehicle[property]); // …print out the values
  10.             }
  11.         }
  12.     }
  13. })("condition");
  14.  
  15.  
  16. // Two properties:
  17. (function(property1,property2) {
  18.     for (var index = 0; index < dataLayer.length; index++) {
  19.         if (dataLayer[index].ajaxLists != undefined) {
  20.             console.log("In dataLayer[" + index + "] we have the following:");
  21.             var theList = dataLayer[index].ajaxLists.vehicles;
  22.             for (var i = 0; i < theList.length; i++) {
  23.                 var theVehicle = theList[i];
  24.                 // console.log( "Make: " + theVehicle.make );
  25.                 console.log(property1 + ": " + theVehicle[property1] + ", " + property2 + ": " + theVehicle[property2] ); // …print out the values
  26.             }
  27.         }
  28.     }
  29. })("upgradeMobileBoost","listingPosition");
  30.  
  31. // v4: Use any number of arguments:
  32. (function() {
  33.     for (var index = 0; index < dataLayer.length; index++) {
  34.         if (dataLayer[index].ajaxLists != undefined) {
  35.             var theList = dataLayer[index].ajaxLists.vehicles;
  36.             for (var i = 0; i < theList.length; i++) {
  37.                 var theVehicle = theList[i];
  38.                 console.log("==== ");
  39.                 console.log(theVehicle.make + " " + theVehicle.model);
  40.                 for (a = 0; a < arguments.length; a++) {
  41.                     console.log(arguments[a] + " = " + theVehicle[arguments[a]]);
  42.                 }
  43.             }
  44.         }
  45.     }
  46. })("adType", "condition", "province", "financingPrice", "transmission");
  47.  
  48. // v5: Add the entry number for visual ease:
  49. (function() {
  50.     for (var index = 0; index < dataLayer.length; index++) {
  51.         if (dataLayer[index].ajaxLists != undefined) {
  52.             var theList = dataLayer[index].ajaxLists.vehicles;
  53.             for (var i = 0; i < theList.length; i++) {
  54.                 var theVehicle = theList[i];
  55.                 console.log(i + " ==== ");
  56.                 console.log(theVehicle.make + " " + theVehicle.model);
  57.                 for (a = 0; a < arguments.length; a++) {
  58.                     console.log(arguments[a] + " = " + theVehicle[arguments[a]]);
  59.                 }
  60.             }
  61.         }
  62.     }
  63. })("adType", "condition", "province", "financingPrice", "upgradeMobileBoost");
  64.  
  65. // v6: restore indication of where in the datalayer we are
  66. (function() {
  67.     for (var index = 0; index < dataLayer.length; index++) {
  68.         if (dataLayer[index].ajaxLists != undefined){
  69.             console.log("We're in dataLayer[" + index + "]"); // ← this line
  70.             var theList = dataLayer[index].ajaxLists.vehicles;
  71.             for (var i = 0; i < theList.length; i++) {
  72.                 var theVehicle = theList[i];
  73.                 console.log(i + " ==== ");
  74.                 console.log(theVehicle.make + " " + theVehicle.model);
  75.                 for (a = 0; a < arguments.length; a++) {
  76.                     console.log(arguments[a] + " = " + theVehicle[arguments[a]]);
  77.                 }
  78.             }
  79.         }
  80.     }
  81. })("adType", "condition", "province", "financingPrice", "upgradeMobileBoost");
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement