Thinking of a better way of doing this - I have these arrays available:
var model1 = ['10', '20', '30', '40','50','60'];
var model2 = ['80', '100', '200', '300','400','500'];
var model3 = ['1', '2', '3', '4','5','6'];
and in my code where I use them I do:
$scope.sli['model1'][0]=0;
$scope.sli['model1'][1]=10;
$scope.sli['model1'][2]=20;
$scope.sli['model1'][3]=30;
$scope.sli['model1'][4]=40;
$scope.sli['model1'][5]=50;
$scope.sli['model1'][6]=60;
for each model to declare them to use later.
What would be a better way to do in a for loop, so I simply pass the model array name, split the array into an index, so if new models are added, they are actually automatically picked up, rather than declaring them individually?
An efficient solution is to first find the sum S of all array elements. Check if this sum is divisible by 3 or not. This is because if sum is not divisible then the sum cannot be split in three equal sum sets. If there are three contiguous subarrays with equal sum, then sum of each subarray is S/3.
Example-2: This example uses the splice() method to split the array into chunks of array. This method removes the items from the original array. This method can be used repeatedly to split array of any size. | Split array into chunks.
Using the copyOfRange() method you can copy an array within a range. This method accepts three parameters, an array that you want to copy, start and end indexes of the range. You split an array using this method by copying the array ranging from 0 to length/2 to one array and length/2 to length to other.
You dont need to assign an array
if model1
has been defined as:
var model1 = ['10', '20', '30', '40','50','60'];
you can simply do
$scope.sli['model1'] = model1
and access individual elements
like $scope.sli['model1'][0]
to get "10"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With