By tahoecoop


2019-10-14 22:14:06 8 Comments

I have an array of arrays, ie [[3, 6, 7, 7, 4], [5, 9], [4, 1, 6], [6, 3, 6], [5, 7, 8, 9]]. I am looking to combine these arrays depending on an interval. For example, if interval is 2, I want to combine the first two of these arrays into one array, then the next two arrays into the next array, then the third two arrays into the next, and so forth.

I have been screwing around with concat and just can't figure out how to accomplish what I'm after. Any assistance is greatly appreciated, thanks in advance!

4 comments

@Scott Sauyet 2019-10-15 00:29:38

One technique would be to break it down into simple reusable functions:

aperture groups an array into subarrays of the given length. flatten combines an array of arrays into a single array. Putting these together let us write a simple function:

const aperture = (n, xs) =>
  xs .length <= n
   ? [xs]
   : [xs .slice (0, n), ...aperture (n, xs .slice (n))]

const flatten = (xss) => 
  xss.flat()

const groupArrays = (n, arrs) =>
  aperture (n, arrs) .map (flatten)

const arrs = [[3, 6, 7, 7, 4], [5, 9], [4, 1, 6], [6, 3, 6], [5, 7, 8, 9]]

console .log (groupArrays (2, arrs))
console .log (groupArrays (3, arrs))

@Code Maniac 2019-10-15 01:43:59

There's a typo in const flatten (xss) => part

@Scott Sauyet 2019-10-15 12:29:28

@CodeManiac: Thanks. Fixed. I really know better than any last-minute "fixes". Renaming combine to flatten, I somehow lost the =. And I guess I didn't test.

@Chris G 2019-10-15 05:10:59

If you iterate over the outer array using forEach or reduce, you can append it to the respective inner array of a new result array by dividing the index by the interval:

let arr = [
  [3, 6, 7, 7, 4],
  [5, 9],
  [4, 1, 6],
  [6, 3, 6],
  [5, 7, 8, 9]
]

// combine using forEach
function combine(a, interval) {
  const res = [];
  a.forEach((subA, ii) => {
    const i = Math.floor(ii / interval); // index of combined array in outer array
    res[i] = (res[i] || []).concat(subA); // (create if not existant yet,) append 
  });
  return res;
}

// combine using reduce
function combineR(a, interval) {
  return a.reduce((res, subA, ii) => {
    const i = Math.floor(ii / interval); // index of combined array in outer array
    res[i] = (res[i] || []).concat(subA); // (create if not existant yet,) append   
    return res;
  }, []);
}

console.log(combineR(arr, 3));

@vgro 2019-10-14 22:42:48

Here is another messy solution:)

test = [[3, 6, 7, 7, 4], [5, 9], [4, 1, 6], [6, 3, 6], [5, 7, 8, 9]];
function concat_arrays(arr, interval) {
  concatenated = [];
  for (i = 0; i < arr.length; i += interval) {
    con = test[i];
    for (j = 1; j < interval && i + j < arr.length; j++) {
      con = con.concat(arr[i+j]);
    }
    concatenated.push(con);
  }
  console.log(concatenated);
}
concat_arrays(test, 3);

@jimboweb 2019-10-14 22:34:17

Try this:

let arr = [[3, 6, 7, 7, 4], [5, 9], [4, 1, 6], [6, 3, 6], [5, 7, 8, 9]]

function combine(interval, arr){
  let rtrn = [];
  let currentArr=[];
  for(let i=0;i<arr.length;i++){
    if(i>0 && i%interval===0){
      rtrn.push(currentArr);
      currentArr = [];
    }
    currentArr = currentArr.concat(arr[i]);
  }
  rtrn.push(currentArr);
  return rtrn;
}

console.log(combine(2,arr));

Related Questions

Sponsored Content

47 Answered Questions

88 Answered Questions

[SOLVED] How do I remove a particular element from an array in JavaScript?

  • 2011-04-23 22:17:18
  • Walker
  • 6084910 View
  • 7586 Score
  • 88 Answer
  • Tags:   javascript arrays

3 Answered Questions

41 Answered Questions

[SOLVED] Loop through an array in JavaScript

33 Answered Questions

[SOLVED] For-each over an array in JavaScript?

58 Answered Questions

[SOLVED] How do I include a JavaScript file in another JavaScript file?

27 Answered Questions

[SOLVED] What does "use strict" do in JavaScript, and what is the reasoning behind it?

41 Answered Questions

[SOLVED] How do I remove a property from a JavaScript object?

16 Answered Questions

[SOLVED] How to insert an item into an array at a specific index (JavaScript)?

86 Answered Questions

[SOLVED] How do JavaScript closures work?

Sponsored Content