A Flash Developer Resource Site

Results 1 to 2 of 2

Thread: [RESOLVED] Array.pop()

  1. #1
    Member
    Join Date
    Feb 2009
    Posts
    49

    resolved [RESOLVED] Array.pop()

    Hey guys, lets say I have a data-grid like this:
    ----------------------------------
    Select | Name | Age |
    ----------------------------------
    checkbox | Bob | 12 |
    ----------------------------------
    checkbox | Tim | 17 |
    ----------------------------------
    checkbox | Joe | 15 |
    ----------------------------------

    If I have a function where for every check box I tick, the function will take the age value of those persons and add them together, combining the age into one larger number.
    PHP Code:
    var combinedAge:int;
    function 
    doIt():void {
       if (
    checkBox.selected) {
          
    combinedAge += int(mygrid.ageColumn);
       }

    So if I tick the check box for Bob and Tim, the function will take the values 12 and 17 and add them together resulting in a value of 29.




    Now, at the same time, with this function, for every check box I tick, the function will take the names of those persons and add them into an array.
    PHP Code:
    var storedNames:Array = new Array;
    var 
    combinedAge:int;
    function 
    doIt():void {
       if (
    checkBox.selected) {
          
    combinedAge += int(mygrid.ageColumn);
          
    storedNames.push(mygrid.nameColumn);
       }

    So if I tick the checkbox for Bob and Tim, the function will take their name values and add them into the 'storedNames' array resulting in (Bob, Tim).




    Now, with this function, for every check box I UN-tick, the function will take the age value of those persons and subtract them from combined total age value.
    PHP Code:
    var combinedAge:int;
    function 
    doIt():void {
       if (
    checkBox.selected) {
          
    combinedAge += int(mygrid.ageColumn);
       } else {
          
    combinedAge -= int(mygrid.ageColumn);
       }

    So if the check boxes are ticked for Bob and Tim, the combinedAge value will be 29. If I un-tick the check box for Bob, his age of 12 will now be subtracted from the combined age total resulting in a value of 17.




    Now what I'm trying to achieve is this: When I un-tick check boxes, not only do I want to take the age values and subtract them from the combined age (which I have achieved in the above snippet). I want to also remove those persons from the the storedNames array.
    PHP Code:
    var storedNames:Array = new Array;
    var 
    combinedAge:int;
    function 
    doIt():void {
       if (
    checkBox.selected) {
          
    combinedAge += int(mygrid.ageColumn);
          
    storedNames.push(mygrid.nameColumn);
       } else {
          
    combinedAge -= int(mygrid.ageColumn);
          
    storedNames.pop();
       }

    As you can imagine, in the above snippet, by using the pop method on the storedNames array, it results in the removal of the LAST name added and not the name corresponding to the un-ticked check box.

    So looking at the grid again, if I ticked Bob and then Tim and then Joe and then went to un-tick Tim, Joe will be removed as he was the last name entered into the array when it is Tim I want to be "popped" from the array.

    How can I write this function in a way that will result in the the removal of the correct persons name for when a check box is un-ticked?





    Note that "mygrid" used in (mygrid.ageColumn) and (mygrid.nameColumn) is actually a longer piece of code used to get the data from the specified column for the currently selected row index.

  2. #2
    Member
    Join Date
    Feb 2009
    Posts
    49
    Aha, a guy over at actionscript.org directing me the right way! What I needed was a splice instead of a pop.

    PHP Code:
    var index:int storedNames.indexOf(mygrid.ageColumn);  //placed in the else statement of the function
    if (index > -1
       
    storedNames.splice(index1); 

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center