0

How would you implement the join multiple arrays in JavaScript?

As an example,


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

should return

[
    [ 1, 2 ],
    [ 2, 3 ],
    [ 3, 4 ],
    [ 4, 5 ],
    [ 5, 6 ],
    [ 6, 7 ],
    [ 7, 8 ],
    [ 8, 9 ]
]
Ashik
  • 11
  • 1
  • 1
  • 1
    Show us what you have tried. SO isn't a free code writing service. The objective here is for you to post your attempts to solve your own issue and others help when they don't work as expected. See [ask] and [mcve] – charlietfl Jun 21 '21 at 10:34
  • Familiarize yourself with [how to access and process nested objects, arrays or JSON](/q/11922383/4642212) and how to [create objects](//developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Object_initializer) and use the available [`Object`](//developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object#Static_methods) and [`Array`](//developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array#Static_methods) methods (both static and on prototype). – Sebastian Simon Jun 21 '21 at 10:34
  • The question is unclear. Normally, a cross join would return an output whose size is a multiple from the different inputs, unlike the example you give. – user711413 Jun 21 '21 at 10:37
  • 3
    [Duplicate](//google.com/search?q=site%3Astackoverflow.com+js+overlapping+array+chunks+of+length+2) of [Split array into overlapping chunks (moving subgroups)](/q/14985948/4642212). – Sebastian Simon Jun 21 '21 at 10:37
  • See also https://stackoverflow.com/questions/8495687/split-array-into-chunks – Scott Sauyet Jun 21 '21 at 16:47
  • But a possible one-liner is `const chunk = (xs, n) => xs.length < n ? [[...xs]] : [xs.slice(0, n), ...chunk (xs.slice(n), n)]` – Scott Sauyet Jun 21 '21 at 16:52

0 Answers0