I have an array of N elements. I'd like to divide in two arrays. The only condition is that the first new array is formed by the first three elements of the original array, and the second new array must contain ALL the other elements.
For example the original array contains ["a","b","c","d","e","f","g","h"]. I'd like to create two new arrays:
first new array: ["a, "b", "c"];
second new array: ["d","e","f", "g","h"].
But my code divide a lot of time the original array. Why?
function MyCtrl($scope) {
$scope.strengthChartData = ["Banana", "Orange", "Apple", "Mango", "asd", "lol", "ahhhaha", "llllll", "ehhehehe", "oooo", "aaaaa", "bbbbbbbb"];
$scope.arrays = [];
var size = 3;
while ($scope.strengthChartData.length > 0)
$scope.arrays.push($scope.strengthChartData.splice(0, size));
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app ng-controller="MyCtrl">
<h3>All elements:</h3>
<br>
<ul>
<li ng-repeat="array in arrays">{{array}}</li>
</ul>
<h3>First array:</h3>
<br>
<ul>
<li ng-repeat="array in arrays[0]">{{array}}</li>
</ul>
<h3>Second array:</h3>
<br>
<ul>
<li ng-repeat="array in arrays[1]">{{array}}</li>
</ul>
</div>