0

I have an ajax call which returns same data

obj[0],obj[1],obj[2],obj[3],obj[4],obj[5],obj[6],obj[7],obj[8],obj[9],obj[10],obj[11]....

So i want to take only obj[0]-obj[3] and obj[4]-obj[7] and obj[8]-obj[11]...

var iterate=0;

for(i=0 ;i<obj.length ; i++)
 {
 alert(obj[iterate].mydata);

 iterate=iterate+3;

 }

here the issue is , it iterates each time so i cant get valjue of obj[4] after obj[3] Any idea ?

13
  • 2
    Is obj supposed to be an array or what? Are you trying to split an array into chunks? I don't see what the difference would be between obj[0]-obj[3] and obj[4]-obj[7] and just obj[0]-obj[7]. Please provide a concrete example of the input and output you (want to) get. The better you describe your problem, the easier it is for us to help you. Commented Jul 11, 2014 at 8:49
  • 1
    You mean you want to slice your array into slices of length 4 ? Commented Jul 11, 2014 at 8:50
  • var obj = data.d ; yes it is an array. Commented Jul 11, 2014 at 8:50
  • 1
    and what to do with the slices? Commented Jul 11, 2014 at 8:50
  • 1
    Like I said, it's not clear. I want to take only ... only what? obj[0] minus obj[3]? Or the sum of obj[0] to obj[3]? You're talking in rebuses. You may also want to slice the original array into smaller arrays, etc. The point is, you have to explain your problem and expected solution in a way so we understand it. Commented Jul 11, 2014 at 9:04

3 Answers 3

1

Continously splicing the array would be easier:

var array = [1,2,3,4,5,6,7,8,9,10];
var slices = [];

while( array.length > 0 )
  slices.push( array.splice(0,3) );

console.log( slices );
Sign up to request clarification or add additional context in comments.

2 Comments

But also a lot more expensive. You are creating a new array (the deleted elements) and reposition the existing elements in the array. Changing this code to .slice would only be a small step.
@FelixKling would for(i=0; i<array.length-1; i++) array.unshift( array.splice(i,3) ); be any less expensive?
0

The following code takes three elements at each interation:

var array = [1,2,3,4,5,6,7,8,9,10];

for (var a = 0; a < array.length; a += 3) {
    // On the last iteration, array[a+1] and array[a+2]
    // will be undefined, because array is of length 10,
    // and 10 isn't divisible by 3 without a remainder.
    console.log(array[a], array[a+1], array[a+2]);
}

Also, in your question you're using i and iterate interchangeably in your code.

4 Comments

Hard to get rep when ppl like you type 20 seconds faster :D
Actually the question was asked 10 minutes ago ;)
@Ms. Nobody: answer questions for [forth] or [typo3] :-) there is relaxing silence :-)
Yeah but it was specified to level where I could finally know what does the OP want about a minute ago :D
0

I think a nested loop will help you. Like this:

for(i=0 ;i<obj.length ; i+=4) {  // << step must be 4, not 3

   for (j=0; (j<=3) && (i+j < obj.length); j++) {
      alert(obj[i + j].mydata); // << here you take 0-3, 4-7 etc.
   }

}

EDIT: termination in outer loop corrected

4 Comments

This will break once iterate > obj.length - 4, which will happen after obj.length / 4 iterations (I think).
I've improved the termination and the usage of counters. Thank you for your hint :-)
this will resolve the issue , Thanks for your cooperation guys . @FelixKling and peter_the_oak
Glad to help :-) Plz consider marking the correct answer as it gives quick orientation for other readers.

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.