Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New Coder
    Join Date
    Mar 2012
    Posts
    78
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Question about how to show last item in an array list

    Hi All,

    This is a question about the code below.

    The code works to give the result wanted. However, I would like to know if it is really a proper code written correctly or just a fluke it worked.

    The need of the code that was being worked on was to show just the last item in an array list from an external source which appears below as the list array input field, with various words and forward slashes just as shown.

    While putting the code together it was tested and unexpectedly showed the wanted result.

    Code:
    The List Array <input id="externalarraylist" size="50" value="Tree/House/Train/Block/DVD/CD/Jewelry/Road">
    
    <BR>
    
    The last item <input id="show_only_Last_one" size="10" value="">
    
    <BR>
    
    <script>
    function DisplayLastOne()
    {
    
    var findlast = document.getElementById("externalarraylist").value.split('/');
    
    for ( i=0; i < findlast.length; i++ )
    {
    
    document.getElementById("show_only_Last_one").value=findlast[i];
    
    }
    
    }
    </script>
    
    <BR>
    
    <button onclick="DisplayLastOne()">See Last One</button>

    Martin.

  • #2
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    3,760
    Thanks
    23
    Thanked 546 Times in 545 Posts
    It works, but is kinda round about. I'd make the array in JS and use pop() not a loop.
    Code:
    The last item <input id="show_only_Last_one" size="10" value="">
    <BR>
    <button onclick="DisplayLastOne()">See Last One</button>
    
    
    
    <script>
    function DisplayLastOne(){
    	var findlast = ['Tree','House','Train','Block','DVD','CD','Jewelry','Road'];
    	document.getElementById("show_only_Last_one").value = findlast.pop();
    }
    </script>
    Reference: http://www.w3schools.com/jsref/jsref_pop.asp
    Last edited by sunfighter; 09-28-2013 at 05:40 PM. Reason: added ref.
    Evolution - The non-random survival of random variants.

  • #3
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,730
    Thanks
    202
    Thanked 2,508 Times in 2,486 Posts
    No need for a loop.

    Code:
    <script type = "text/javascript">
    function DisplayLastOne() {
    var findlast = document.getElementById("externalarraylist").value.split('/');
    var len = findlast.length;
    document.getElementById("show_only_Last_one").value=findlast[len-1];
    }
    </script>

    Quizmaster: How many dwarves were there in the Snow White story?
    Contestant: Ten.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #4
    Regular Coder
    Join Date
    Mar 2006
    Posts
    725
    Thanks
    35
    Thanked 132 Times in 123 Posts
    You don't need to create an array to locate a part of a string-

    Code:
    function DisplayLastOne(str){
    	var findlast= document.getElementById("externalarraylist").value;
    	document.getElementById("show_only_Last_one").value= 
    	findlast.substring(findlast.lastIndexOf('/')+1);
    }

  • #5
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,730
    Thanks
    202
    Thanked 2,508 Times in 2,486 Posts
    Quote Originally Posted by mrhoo View Post
    You don't need to create an array to locate a part of a string-
    As we have often said, there are many possible ways to skin a cat. The advantage of my script is that it would be easy to find the next-to-last item, the second-from-last or whatever.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.


  •  

    Posting Permissions

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