1

I have an array as follows:

var dataPushArray = [
 {
      "name" : "name1",  //Name for reference
      "event": "event1",  //Event to be passed
      "catagory": "catagory1",  //Catagory to be passed
      "action": "action1",  //Action to be passed
      "label": "label1",  //Label(s) to be passed
      "trigger": "views-ac-dependent-filter",  //Class or ID of click 
element

  },
 {
      "name" : "name2",  //Name for reference
      "event": "event2",  //Event to be passed
      "catagory": "catagory2",  //Catagory to be passed
      "action": "action2",  //Action to be passed
      "label": "label2",  //Label(s) to be passed
      "trigger": "edit-submit-hiscox-marklerportal-search",  //Class or ID of click element
  }
];

And I have a click event that looks at all the "triggers" in the array and if one is clicked do something:

    //Define vars
var clickTriggers = [];
var pushData = "";

//Loop through all results
for(i=0;i<dataPushArray.length;i++){

    //Merge click selector with click trigger

    var trigger = String(dataPushArray[i].trigger);

    //Puts all individual click triggers in clickTriggers array
    clickTriggers.push(trigger);

}   

//Click function for all click triggers
$(clickTriggers.join(',')).on('click', function(trigger) {
    if(jQuery.inArray(trigger, dataPushArray)) {
        //Output data here
    }
})

Which works fine but what I am trying to do is get all values from the array after the specific element is clicked for instance if an element with the trigger "views-ac-dependent-filter" is clicked I then want to get the name, event, catagory, and label from that part of the array.

Any help on this would be hugely appreciated

David
  • 23
  • 5
  • It would help to show us where `clickTriggers` is defined. – Mitya Nov 12 '18 at 11:06
  • Sorry code now updated I didnt want it to get overwhelming when reading – David Nov 12 '18 at 11:08
  • That's a worthy sentiment - too many questions have entire code dumps in them - but always include *pertinent* code. – Mitya Nov 12 '18 at 11:10
  • If I did understand the question right you are basically trying to get the object from dataPushArray knowing one of its properties (trigger). you may use a function like the one used here (https://stackoverflow.com/questions/5579678/jquery-how-to-find-an-object-by-attribute-in-an-array). From the object returned you can then access its properties – Sorin87bv Nov 12 '18 at 11:55

0 Answers0