Search code examples
javascriptjquerycountarr

pushing obj to array


I've created a series of objects, and grouped them up , into 1 item,

I'm trying to create an array of those objects, but there's 2 little problems with my code.

  1. the eventName doesn't increment like I thought I coded it to, it does when I isolate that part out, and

  2. it recognizes the correct information, but when I push it to the array, its always at [0]...just wondering what I'm doing wrong?

I hope its ok, if I just use the fiddle I created, instead of copy paste all over the place.

$(document).ready(function() {
  showBreadCrumb();
});

function showBreadCrumb() {
  $("a").click(function() {
    let idzName = ($(this).attr('id'));
    let classzName = ($(this).attr('class'));
    let clickRecord = $(this).data('clicked', true);
    let count = 0;
    let eventName = makeName();
    let eventObject = makeEventObject(eventName, idzName, classzName);
    let eList = makeEventList(eventObject);


    function makeName() {

      let vName = "event" + (++count);
      let countO = new nameCount(vName, count);
      //console.log(countO,vName);

      return vName;
    }
    eventName = makeName();


    function nameCount(vName, count) {
      this.eName = vName;
      this.counter = count;
    }

    function makeEventObject(eventName, idzName, classzName) {

      function eventDetails(eventName, idzName, classzName) {
        this.eventName = eventName;
        this.idzName = idzName;
        this.classzName = classzName;
      }
      let eventObject = new eventDetails(eventName, idzName, classzName);
      // console.log(eventObject);
      return eventObject;
    }
  })

  function makeEventList(eventObject) {
    let eventList = [];
    eventList.push(eventObject);
    console.log(eventList);
  }
}

Fiddle URL


Solution

  • I dont know why all this imbricated inner functions , but if you want to store all click event in an array , you have to decalre your assign you array outside your fuction to prevetn reinitazing it , same thing for counter .

    See below working snippet : (Ps deleted usless method + decalre others outside of click scope ( why declaring function in memory every click .. )

    $(document).ready(function() {
      eventList = [];
      count = 0;
      showBreadCrumb();
    });
    
    function showBreadCrumb() {
      $("a").click(function() {
        let idzName = ($(this).attr('id'));
        let classzName = ($(this).attr('class'));
        let clickRecord = $(this).data('clicked', true);
        let eventName = makeName();
        let eventObject = makeEventObject(eventName, idzName, classzName);
        let eList = makeEventList(eventObject);
      });
    }
    
    
    function makeEventObject(eventName, idzName, classzName) {
    
      function eventDetails(eventName, idzName, classzName) {
        this.eventName = eventName;
        this.idzName = idzName;
        this.classzName = classzName;
      }
      let eventObject = new eventDetails(eventName, idzName, classzName);
      // console.log(eventObject);
      return eventObject;
    }
    
    function makeName() {
    
      let vName = "event" + (++count);
      return vName;
    }
    
    function makeEventList(eventObject) {
      eventList.push(eventObject);
      $(".as-console").html("");
      console.log(eventList);
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <div id="breadcrumb">Start @ Home</div>
    
    <div class="menubar">
      <ul id="navigation_links">
        <li><a href="#" id="A" class="L1">Page A</a></li>
        <li><a href="#" id="B" class="L1">Page B</a></li>
        <li><a href="#" id="C" class="L1">Page C</a></li>
        <li><a href="#" id="D" class="L1">Page D</a></li>
        <li><a href="#" id="E" class="L1">Page E</a></li>
        <li><a href="#" id="F" class="L1">Page F</a></li>
        <li><a href="#" id="G" class="L1">Page G</a></li>
        <li><a href="#" id="H" class="L1">Page H</a></li>
      </ul>
    </div>