1

I have an array-like:

[1,2,3,4,5,6,7]

and I want to split this array into groups so I write:

Object.defineProperty(Array.prototype, 'chunk_inefficient', {
  value: function(chunkSize) {
    var array = this;
    return [].concat.apply([],
      array.map(function(elem, i) {
        return i % chunkSize ? [] : [array.slice(i, i + chunkSize)];
      })
    );
  }
});

Using var pages = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10].chunk_inefficient(3); I got arrays like:

[[1,2,3], [4,5,6], [7,8,9], [10]]; and all its ok.

How I can update my function to keep creating arrays like this but etc. 2nd array to create with 2 elements;

var choose_array = 2;
var choose_elements_in_array = 2;

based om this 2 variables I need to change 2nd array to contain only 2 elements so I need to get array like:

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

if

 var choose_array = 1;
    var choose_elements_in_array = 2;

then array needs to be like:

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

If it easier how to recreate array [[1,2,3], [4,5,6], [7,8,9], [10]]; to [[1,2,3], [4,5], [6,7,8], [9,10]]; based on my variables

var choose_array = 1;
        var choose_elements_in_array = 2;

1 Answer 1

2

You could use an additional rules object which has the custom chunk size for each index in the output. If it is not defined for that index, use the default chunk size:

function conditionalChunk(array, size, rules = {}) {
  let copy = [...array],
      output = [],
      i = 0;

  while (copy.length)
    output.push( copy.splice(0, rules[i++] ?? size) )

  return output
}

For the below rules object

{
   0: 4,
   1: 2
}

The output will be:

[[1, 2, 3, 4], [5, 6], [7, 8, 9], [10]]

Here's a snippet:

function conditionalChunk(array, size, rules = {}) {
  let copy = [...array],
      output = [],
      i = 0;

  while (copy.length)
    output.push( copy.splice(0, rules[i++] ?? size) )

  return output
}

console.log(  
  JSON.stringify( conditionalChunk([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], 3, { 1: 2 }) ) 
)

console.log(
  JSON.stringify( conditionalChunk([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], 3, { 0:4, 1:2 }) )
)

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

10 Comments

Thank you very much. Works very well. Please just a suggestion - WHat is I have an array like: [[1,2,3], [4,5,6], [7,8,9], [10]]; and I just want to change 2 arrays to contain 2 products?
@AleksPer are you always going to reduce the number of items in the array? Like, you only want to make it 2 and not 4.
no, sometimes I want to reduce to 2 elements, sometimes I need 4
@AleksPer the simplest way is to call the above conditionalChunk with array.flat() as input array
Sorry for boring you but what if I have an input array like: [{'page_layout': 1, 'theme': 0, 'items', [1,2,3]}, {'page_layout': 5, 'theme': 1, 'items', [4,5,6,7]}] ... because chunk is actually pages and I need to put more details before each chunk
|

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.