Question

I'm trying to understand where i'm going wrong here. say I have an array:

result = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]      

I want to get rid of every 6 entries in my array, leave the 7th there, and go one. In effect i'm hoping to get rid of 6/7th of the values in my array. I'm thinking splice() could be perfect for that, but somehow the code below doesn't seem to work. Any suggestions?

Many thanks to you all!

function random_select ()
    {
      for(var i=0; i < result.length; i+7)
      {
        result.splice[i,6];

      }
    };
Était-ce utile?

La solution

Try this instead:

var result = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16];
for (var i = 0; i < result.length; i++) result.splice(i, 6);

This will:

  1. First remove [0, 1, 2, 3, 4, 5], and let [6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16] be.
  2. Next remove [7, 8, 9, 10, 11, 12] and let [13, 14, 15, 16] be.
  3. Finally remove [13, 14, 15, 16].

Hence the result is [6, 13]. Is this what you want?

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top