0

I need help in somewhere in javascript

var obj = jQuery.parseJSON(data.d); 

This ajax call result returns some data (around 31).

Here is an example:

obj[0] = 10, obj[1]=20 ,obj[2]=30 , obj[4]=21,obj[5]=16,obj[6]=54 here I want to get value of

obj[0] and ob[4] using for loop . And I also need to do this for obj[10] and obj[14] the difference will be 5 between the i values .

Any idea or help?

9
  • 7
    Why would you use a for loop if you know you want 0 and 4? just get 0 and 4. (or 8 according to your title....) Commented Jul 10, 2014 at 19:29
  • Ah, so you want every 5th. that makes a lot more sense. Though i still dont know where 8 in your title comes in. Commented Jul 10, 2014 at 19:31
  • I want to use same process for others . you know , the Difference will be 5 between I values Commented Jul 10, 2014 at 19:32
  • yes sorry I forgot to update it Commented Jul 10, 2014 at 19:33
  • You want obj[0] and then obj[4] .. then the difference is 4 and not 5. right? Commented Jul 10, 2014 at 19:38

3 Answers 3

1

You can do something like this (a pseudo code)

int i = 0;

while(i < 31)
{
 print(i);
 i = i + 4; 
}
Sign up to request clarification or add additional context in comments.

3 Comments

definitely should be +5, since he always wants 0 and 4. 0,4,10,14,20,24,30
@KevinB, home come? to me it looks like a skip of 4 index and not 5.
@KevinB, accepted. Unless OP clarifies it, it's indeed strange.
1

If i understood, you want to skip 5 on each step:

var array = 'abcdefghijklmnopqrstuvwxyz'.split(''); // something that generates an array just to show an example

var i = 0;

for (; i < array.length;) {
    console.log(i, array[i]);
    i += 5;
}

see fiddle

Comments

1
var iterator=0;
for (i = 0; i < obj.length; i++) { 
    if(i == iterator){
       alert(obj[iterator]);
       iterator= iterator + 4;
    }
}

1 Comment

There is actulally small issue I am having. since I want to get obj0 and obj4 also obj5 and obj9 , it makes problem when I try to get obj5 because its adding +4 each time

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.