NonplayerCharacter

IIFE | Verify that values of a given property are in array

Apr 28th, 2017 (edited)
522
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Verify values
  2. (function(prop){
  3.         var vL = dataLayer[0].lists[0].vehicles, validOptions = [ "dealer", "private" ], verdict, exceptions = 0;
  4.         for ( i = 0; i < vL.length; i++ ){
  5.            console.log( vL[ i ][ prop ] );
  6.            if ( validOptions.indexOf( vL[ i ][ prop ] ) === -1 ){ exceptions += 1;}
  7.         }
  8.         verdict = exceptions > 0 ? exceptions + " invalid values" : "All good";
  9.         console.log( verdict );
  10. })("adType");
  11.  
  12.  
  13. // Three properties at once:
  14. (function(property1,property2, property3) {
  15.     for (var index = 0; index < dataLayer.length; index++) {
  16.         if (dataLayer[index].lists != undefined) {
  17.             var theList = dataLayer[index].lists[0].vehicles;
  18.             for (var i = 0; i < theList.length; i++) {
  19.                 var theVehicle = theList[i];
  20.                 console.log(property1 + ": " + theVehicle[property1] + ", " + property2 + ": " + theVehicle[property2] + ", " + property3 + ": " + theVehicle[property3] ); // …print out the values
  21.             }
  22.         }
  23.     }
  24. })("adType","condition", "province");
  25.  
  26. // Neatified output:
  27. (function(property1, property2, property3) {
  28.     if (dataLayer[0].lists != undefined) {
  29.         var theList = dataLayer[0].lists[0].vehicles;
  30.         for (var i = 0; i < theList.length; i++) {
  31.             var theVehicle = theList[i];
  32.             console.log("===== " + theVehicle.make + " " + theVehicle.model + " =====");
  33.             for (a = 0; a < arguments.length; a++) {
  34.                     console.log(arguments[a] + " = " + theVehicle[arguments[a]]);
  35.                 }
  36.             }
  37.         }
  38. })("adType", "condition", "province");
  39.  
  40. // v4: Use any number of arguments:
  41. (function(){
  42.     if (dataLayer[0].lists != undefined) {
  43.         var theList = dataLayer[0].lists[0].vehicles;
  44.         for (var i = 0; i < theList.length; i++) {
  45.             var theVehicle = theList[i];
  46.             console.log("==== " + theVehicle.make + " " + theVehicle.model + " ====");
  47.             for (a = 0; a < arguments.length; a++) {
  48.                     console.log(arguments[a] + " = " + theVehicle[arguments[a]]);
  49.                 }
  50.             }
  51.         }
  52. })("adType", "condition", "province", "financingPrice");
  53.  
  54.  
  55. // v5: we don't know where our list is
  56. (function(){
  57.     for(var m=0; m<dataLayer.length; m++){
  58.         if (dataLayer[m].lists != undefined){
  59.             for(var n=0; n<dataLayer[m].lists.length; n++){
  60.                 var theList = dataLayer[m].lists[n].vehicles;
  61.                 for (var i=0; i<theList.length; i++){
  62.                     var theVehicle = theList[i];
  63.                     console.log(i + ("@").repeat(30));
  64.                     console.log(theVehicle.make + " " + theVehicle.model);
  65.                     for (var a=0; a<arguments.length; a++) {
  66.                             console.log(arguments[a] + " = " + theVehicle[arguments[a]]);
  67.                         }
  68.                     }
  69.                 }
  70.             }
  71.         }
  72. })("adType", "condition", "province", "financingPrice");
Advertisement
Add Comment
Please, Sign In to add comment