Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 06-16-2009, 07:33 AM   PM User | #1
3lementalist
New to the CF scene

 
Join Date: Jun 2009
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
3lementalist is an unknown quantity at this point
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
3lementalist is offline   Reply With Quote
Old 06-16-2009, 08:49 AM   PM User | #2
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,098
Thanks: 197
Thanked 2,421 Times in 2,399 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
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
Philip M is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 09:01 AM.


Advertisement
Log in to turn off these ads.