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

4
  • What would 1-11 output? what about 7-19? Please give examples of other inputs and outputs? Commented May 4, 2022 at 13:56
  • Alright, I'll edit the question. Commented May 4, 2022 at 14:00
  • You can chunk the array into 5's with this question, then for each chunk, wrap the last 3 in another array. Commented May 4, 2022 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) Commented May 11, 2022 at 0:47

1 Answer 1

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!

Sign up to request clarification or add additional context in comments.

8 Comments

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 =>)?
That shouldn't matter. If you read the first answer in the question I linked the answer uses simple traditional loops.
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"?
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.
Still refuses to work. I did this and it gave me the error "[object Array] is not a function".
|

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.