Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

split multiple arrays into variables based on name

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?

like image 634
Poiro Avatar asked Jul 04 '15 11:07

Poiro


People also ask

How do you split an array into 3 parts?

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.

How do you split an array of elements?

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.

How do you subdivide an array in Java?

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.


1 Answers

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"

like image 90
nalinc Avatar answered Sep 20 '22 01:09

nalinc