How to return multiple arrays from a function in Javascript?

前端 未结 3 1894
广开言路
广开言路 2021-02-14 09:29

I have multiple arrays in a function that I want to use in another function. How can I return them to use in another function

this.runThisFunctionOnCall = functi         


        
3条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-02-14 10:15

    as an array ;)

    this.runThisFunctionOnCall = function(){
        var array1 = [11,12,13,14,15];
        var array2 = [21,22,23,24,25];
        var array3 = [31,32,33,34,35];
    
        return [
         array1,
         array2,
         array3
        ];
    }
    

    call it like:

     var test =  this.runThisFunctionOnCall();
     var a = test[0][0] // is 11
     var b = test[1][0] // is 21
     var c = test[2][1] // is 32
    

    or an object:

    this.runThisFunctionOnCall = function(){
        var array1 = [11,12,13,14,15];
        var array2 = [21,22,23,24,25];
        var array3 = [31,32,33,34,35];
    
        return {
         array1: array1,
         array2: array2,
         array3: array3
        };
    }
    

    call it like:

     var test =  this.runThisFunctionOnCall();
     var a = test.array1[0] // is 11
     var b = test.array2[0] // is 21
     var c = test.array3[1] // is 32
    

提交回复
热议问题