Call a function that takes parameters and returns two values

0

I have done several searches on the Internet, and I have found how to return two values in a function, and then how to access them, but what I need is to use a function to which a parameter passed and which in turn returns two values, I know this can be done in PHP, but I do not know how to do it in javascript . Let me explain myself a little better:

function ObtenerDate(valor){
  ....
  var valor_final = getHours();
  var date = getDate();

  return [valor_final , date ]
}

Later I call the GetDate () function

 for(i = 0; i<array.length; i++){
    var values = ObtenerDate();
    arreglo[i] = values[0](array[i]);  //ESTO NO ME FUNCIONA

}
  

The problem is when I call the GetDate function, accessing the   position 0 or 1 I get data, but I do not know how to pass parameters that   I need it for the function to work. I need to do for because in each cycle of for I call the function GetDate ()

EDIT (Adding information)

The variable array found in the for is where I store a set of dates in the UNIX EPOC type.

In the ObtenerDate() function is where I convert the date of array to the Y-m-d

format

In arreglo[i] is where I save the date already converted

Thanks in advance.

    
asked by Mary 22.03.2018 в 13:57
source

3 answers

0

If you want to get two results or more than one function, instead of making an array, you should do, what is said in PHP, a indexed array but in javascript it is a object.

function ObtenerDate(valor) {
  ...
  var resultado = {
    valor_final: getHours(),
    date: getDate()
  };

  return resultado;
}

Then in the cycle for :

for (i = 0; i < array.length; i++) {
  var values = ObtenerDate('valorParametro');
  // asigno a la variable arreglo[i] el "valor_final" de la función
  arreglo[i] = values.valor_final;

  // asigno a la variable arreglo[i] el "date" de la función
  arreglo[i] = values.date;
}
    
answered by 22.03.2018 / 14:35
source
2

The error it throws is:

  

TypeError: values [0] is not a function

Which means that a call is being made to a method that does not exist and that you do here: values[0](array[i]); .

According to samples in your example, the function Date() returns values, not functions but you are accessing the value as if it were a function.

Are you trying to change the value of position 0 of values ? Then it should be like this:

values[0] = array[i];

Verify that your logic is OK since obviously values does not contain any reference to a function in position 0.

    
answered by 22.03.2018 в 14:05
-2

Greetings friend what you could do is create a function where you send as parameter a arreglo[] with all the data you need and in this function you enter as a response arrangement, so you would not have to use the for and you could have all the answers you need.

function Date[] ObtenerDate(String[] valor){
  date[] resp;
  resp[0]= getHours().;
  resp[1] = getDate();

  return resp
}
// luego lo llamarias algo asi por ejemplo
String[] valor;
valor[0] = "a";
Date[] arreglo = ObtenerDate(valor) ;
// no si es lo que necesitas pero podria ser una opcion
    
answered by 22.03.2018 в 14:40