Split array into two arrays

前端 未结 9 948
甜味超标
甜味超标 2020-12-24 10:24
var arr = [\'a\', \'b\', \'c\', \'d\', \'e\', \'f\'];
var point = \'c\';

How can I split the \"arr\" into two arrays based on the \"point\" variabl

9条回答
  •  猫巷女王i
    2020-12-24 11:25

    When splitting the array you are going to want to create two new arrays that will include what you are splitting, for example arr1 and arr2. To populate this arrays you are going to want to do something like this:

    var arr1, arr2; // new arrays
    int position = 0; // start position of second array
       for(int i = 0; i <= arr.length(); i++){
           if(arr[i] = point){ //when it finds the variable it stops adding to first array
               //starts adding to second array
                for(int j = i+1; j <= arr.length; j++){
                   arr2[position] = arr[j];
                   position++; //because we want to add from beginning of array i used this variable
                }
           break;
           }
          // add to first array
           else{
               arr1[i] = arr[i];
           }
    }
    

    There are different ways to do this! good luck!

提交回复
热议问题