DzonyB,
<!DOCTYPE html>
<html>
<head>
<title>Untitled</title>
<meta charset="utf-8">
<script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
</head>
<body>
<div id="app">
<ul>
<li v-for="el of array_out">
{{el}}
</li>
</ul>
</div>
<script>
const webstore = new Vue({
el: '#app',
data() {
return {
array_in: [1, 2, 4, 7, 1, 6, 2, 8],
count_group: 3,
array_out: []
}
},
created() {
this.array_out = this.getArray(this.array_in, this.count_group);
},
methods: {
getArray(arr, size) {
arr = arr.slice().sort((a, b) => b - a);
let arr_current = [];
let arr_calculated = [arr_current];
let average = (arr, size) => Math.trunc(arr.reduce((x, y) => x + y) / size);
for (let i = 0; size; i++) {
let limit = average(arr, size--);
for (let k = 0; k < arr.length;) {
if (limit - arr[k] >= 0) {
limit -= arr[k];
arr_current.push(arr[k]);
arr.splice(k, 1);
} else k++;
}
if(size == 1){
arr_calculated.push(arr);
break;
}
if (arr.length) {
arr_current = [];
arr_calculated.push(arr_current);
}
}
return arr_calculated;
} // End Get array
},
}); //End Vue
</script>
</body>
</html>