0

I want to filter a large array list into multiple arrays for every 5 items in a certain way so that [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] would be [[1, 2, [3, 4, 5]], [6, 7, [8, 9, 10]]] or [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15] would be [[1, 2, [3, 4, 5]], [6, 7, [8, 9, 10]], [11, 12, [13, 14, 15]]]. (All arrays will be a multiple of 5 in my program.)

How would I do this?

Right now I'm doing this

for (var i = 1; i < (stoneTextureUnfiltered.length+1)/1.01; i++) {
    stoneTexture.push([stoneTextureUnfiltered[i], stoneTextureUnfiltered[i+1], stoneTextureUnfiltered[i+2], [stoneTextureUnfiltered[i+3], stoneTextureUnfiltered[i+4], stoneTextureUnfiltered[i+5]]]);
}

but it doesn't seem to be working.

Thanks,

-Voxel

isherwood
  • 58,414
  • 16
  • 114
  • 157
Voxel
  • 64
  • 9
  • What would 1-11 output? what about 7-19? Please give examples of other inputs and outputs? – evolutionxbox May 04 '22 at 13:56
  • Alright, I'll edit the question. – Voxel May 04 '22 at 14:00
  • You can chunk the array into 5's with this [question](https://stackoverflow.com/questions/8495687/split-array-into-chunks), then for each chunk, wrap the last 3 in another array. – kelsny May 04 '22 at 14:01
  • I discontinued Processing.js in December of 2018. You should not be using it for new projects (instead, give p5js a try. Especially given the legacy JS you're showing: we no longer use `var` because it's _really weirdly scoped_. Either use `const` for variables that should not get reassigned, or `let` if you need to reassign values to it) – Mike 'Pomax' Kamermans May 11 '22 at 00:47

1 Answers1

0

Assuming you've chunked the array already into parts of 5 with these answers and it's stored in a variable named chunks, to wrap the last 3 in each chunk you can use map:

const final = chunks.map((chunk) => [chunk[0], chunk[1], chunk.slice(2)]);

You add the first and second elements to the new list, then add the rest of the chunk as a whole.

Demo below:

// using second answer
var perChunk = 5 // items per chunk    

var inputArray = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]

var chunks = inputArray.reduce((resultArray, item, index) => { 
  const chunkIndex = Math.floor(index/perChunk)

  if(!resultArray[chunkIndex]) {
    resultArray[chunkIndex] = [] // start a new chunk
  }

  resultArray[chunkIndex].push(item)

  return resultArray
}, [])

// answer below

const final = chunks.map((chunk) => [chunk[0], chunk[1], chunk.slice(2)]);

console.log(final);

As you can see, it works nicely!

kelsny
  • 23,009
  • 3
  • 19
  • 48
  • Unfortunately, the program I'm using doesn't support ES6. It's really annoying, but is there a way I can do this without ES6 syntax (such as =>)? – Voxel May 04 '22 at 14:35
  • That shouldn't matter. If you read the first answer in the question I linked the answer uses simple traditional loops. – kelsny May 04 '22 at 16:01
  • const final = chunks.map((chunk) => [chunk[0], chunk[1], chunk.slice(2)]); uses ES6 syntax (const and =>), which the program I'm using doesn't support. What do you mean it "shouldn't matter"? – Voxel May 04 '22 at 16:34
  • You can convert that to `var final = chunks.map(function mapper(chunk) { return [chunk[0], chunk[1], chunk.slice(2)]; });`, which does not rely on any ES6 features. – kelsny May 04 '22 at 16:35
  • Still refuses to work. I did [this](https://pastebin.com/FBWaVXWY) and it gave me the error "[object Array] is not a function". – Voxel May 06 '22 at 14:00
  • @Voxel [Demo not using ES6](https://tsplay.dev/NlvVxW). Click "Run" and see the logs to the right. – kelsny May 06 '22 at 14:03
  • Huh, when I just copy-pasted that it worked... wonder why my version didn't work, but regardless, thanks a lot!! – Voxel May 06 '22 at 14:12
  • @Voxel Doesn't sound like a language-specific problem to me. I think your environment is just foobar lol. – kelsny May 06 '22 at 14:20