0

I would like to create a two-dimensional array based on targetItems with the number of numbers in splitNumber and output it as follows.

const targetItems = [1, 2, 3, 4, 5, 6, 7, 8, 9];
const splitNumber = 2;

We are looking for the following results.

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

Is there a good way?

I was thinking of using Math.round, etc. to carry it out if it can't be done evenly. If the number of targetItems is 5 and the splitNumber is 2

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

If the number of targetItems is 17 and the splitNumber is 2

[[1,2,3,4,5,6,7,8,9], [10,11,12,13,14,15,16,17]]

If the number of targetItems is 5 and the splitNumber is 3

[[1,2], [3,4], [5]]
  • Does it have a consistent behavior when the number cannot be evenly divided? Like, if 100 is divided into 6, should the program give you `16,17,17,16,17,17`, or `17,17,17,17,16,16` or `16,16,17,17,17,17` or use some other order? – qrsngky Jul 06 '22 at 11:01
  • I was thinking of using Math.round, etc. to carry it out if it can't be done evenly. If the number of targetItems is 5 and the splitNumber is 2 [[1,2,3], [4,5]] If the number of targetItems is 17 and the splitNumber is 2 [[1,2,3,4,5,6,7,8,9], [10,11,12,13,14,15,16,17]] If the number of targetItems is 5 and the splitNumber is 3 [[1,2], [3,4], [5]] – december964 Jul 06 '22 at 12:10

4 Answers4

1

Basically, we use the arary.slice(start,end) method splitNumber of times, having the bigger parts first. The results depend indeed on how you define the problem in the first place. So this code might need changes.

const targetItems = [1, 2, 3, 4, 5, 6, 7, 8, 9];

function split(targetItems, splitNumber) {
  const half = Math.ceil(targetItems.length / splitNumber);

  var parts = [];
  for (var i = 0; i < splitNumber; i++) {
    parts.push(targetItems.slice(i * half, i * half + half))
  }
  return parts;
}


for (var i = 1; i <= targetItems.length; i++) {
  console.log("split to " + i + " parts", JSON.stringify(split(targetItems, i)))
}
.as-console-wrapper {
  max-height: 100% !important;
}
IT goldman
  • 14,885
  • 2
  • 14
  • 28
  • I get that you use `ceil`, but it's strange to see, in case of a 4 way split, 3,3,3,0 items. Would there be a way to do it that creates a more even split? Would `round` work? – fravolt Jul 06 '22 at 11:28
  • Also, one of the other answers is literally a carbon copy of yours – fravolt Jul 06 '22 at 11:28
  • I think you should use `floor` instead of `ceil`, that makes the splits a lot more even :) oh no, never mind! That does not work at all. It would need to be more complex to make a more even split I guess. – fravolt Jul 06 '22 at 11:45
  • (sorry for the spam haha) I guess a 'fair' divison there would be 3,2,3,2, when everything is properly rounded, but once again, the code will become more complex to achieve that – fravolt Jul 06 '22 at 11:47
  • No it's ok. Specifically `floor` and `ceil` don't work here, but it's a challenge to make it like 3,2,3,2 for 10/4. – IT goldman Jul 06 '22 at 11:53
0

I used this code in my previous project:

var arr = [1,2,3,4,5,6,7,8,9,0]
const arr1l = Math.floor(arr.length/2)
  const arr2l = arr.length-arr1l
  const arr1 = arr.slice(0,arr1l)
  const arr2 = arr.slice(arr1l,arr.length)
var new_arr=[arr1,arr2]
document.querySelector(".h1").innerHTML=arr1
document.querySelector(".h2").innerHTML=arr2
<p class="h1"></p>
<p class="h2"></p>
Ge1p
  • 35
  • 2
  • 7
0

I used in this way.

const arr = [1,2,3,4,5,6,7,8,9,1,1,2];

function turnIntoTwoDimension(arr) {
  let twoDimension = []
  
  twoDimension.push(arr.slice(0,arr.length/2))
  twoDimension.push(arr.slice(arr.length/2))
  
  return twoDimension
}
console.log(turnIntoTwoDimension(arr));
-1

const targetItems = [1, 2, 3, 4, 5, 6, 7, 8, 9];

function split(targetItems, splitNumber) {
  const half = Math.ceil(targetItems.length / splitNumber);

  var parts = [];
  for (var i = 0; i < splitNumber; i++) {
    parts.push(targetItems.slice(i * half, i * half + half))
  }
  return parts;
}


for (var i = 1; i <= targetItems.length; i++) {
  console.log("split to " + i + " parts", JSON.stringify(split(targetItems, i)))
}
.as-console-wrapper {
  max-height: 100% !important;
}