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 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Jun 2009
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    How to get a constant variable of array

    hi i hav an array named contents [i] where i wanna get every 3rd value out of it.below i make an example:

    contents [i] = [2,4,6,8,10,12,14,16,18];

    i wanna find a script dat could return every third value which is 3,6,9
    so any1 noe how to do it? thanks

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,080
    Thanks
    203
    Thanked 2,542 Times in 2,520 Posts
    Try this:-

    Code:
    <script type = "text/javascript">
    var contents = [2,4,6,8,10,12,14,16,18,21];
    var result = [];
    var count = 0;
    for (var i =  2; i<contents.length; i= i+3) {
    result[count] = contents[i];
    count++;
    }
    
    alert (result);  // 6,12,18
    </script>

    "Funding is to be cut for sports where targets have been missed, such as archery". - Government circular


  •  

    Posting Permissions

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