Problem with the Spread operator in Javascript

1

I'm trying to put together a function that needs to return the absolute minimum of the values within the arrays that it receives per parameter so it put together the function below.

   var arreglosTest1 = [21,9,34],
        arreglosTest2 = [9,8,13];

    function minimoAbsoluto(...arreglos){
        return Math.min(...arreglos);
    }

It is assumed that if I parameterize the arrays that I put in the variables I would have to return "8", but it returns "NaN". Did someone suffer the same problem or know why that value returns?

Thank you very much.

    
asked by Aaron 21.07.2018 в 20:35
source

2 answers

1

I added all the values in the same array look for the least value and wrote this code:

miarray = function(valor) {
    var minimum = Math.min(...valor);
    console.log(minimum);
}

miarray([9,8,13],[21,9,34]);

and the value it gives me is the

8

but if you want something similar to what you could do something like this:

you could create something like this:

miarray = function(valor) {
    var minimum = Math.min(...valor);
    console.log(minimum);
}
var array1 = [9,8,13];
var array2 = [21,9,34];
miarray(array1,array2);

If you have any questions you can comment.

all the code run it online from: jsbin

Update you could use the concat () function of js to join the array and so look for the smaller one in this way:

miarray = function(valor) {
    var minimum = Math.min(...valor);
    console.log(minimum);
}
var array1 = [9,8,13];
var array2 = [21,9,34];
miarray(array2.concat(array1));

concat

    
answered by 21.07.2018 / 20:55
source
1

There are two concepts at stake here, the first one is "rest params"

function minimoAbsoluto(...arreglos) {

the use of " ...arreglos " means that all parameters that do not yet have a name / assigned variable are put together in an array " arreglos ", if you want to name the first parameter and group the rest:

function minimoAbsoluto( a, ...arreglos) {

I would take the first parameter in " a " and the rest in an array " arreglos "

Second concept "spread syntax"

    return Math.min(...arreglos);

In this case the use of " ...arreglos " scatters the elements of "fixes" separated by commas. Because in the first " ...arreglos " (rest params) you put several arrays in one, when you scatter them, they are still arrays separated by commas.

var arreglosTest1 = [21, 9, 34],
  arreglosTest2 = [9, 8, 13];

function minimoAbsoluto(...arreglos) {
  console.log("arreglos=", arreglos); 
  console.log("...arreglos=", ...arreglos); 

  b = Math.min(...arreglos);
  return b;
}

a = minimoAbsoluto(arreglosTest1, arreglosTest2);

console.log("a=", a);

Result

arreglos= 
[[21,9,34],[9,8,13]]

...arreglos= 
[21,9,34] 
[9,8,13]

a= 
NaN

That is to say that b = Math.min(...arreglos); is being equivalent to b = Math.min([21,9,34],[9,8,13]); which gives NaN .

To concatenate the arrays (and then use the spread for Math.min ) there are several ways one is with reduce

var arreglosTest1 = [21, 9, 34],
  arreglosTest2 = [9, 8, 13];

function minimoAbsoluto(...arreglos) {
  var c = arreglos.reduce((acc, val) => [...acc, ...val]);
  console.log(c);
  var b = Math.min(...c);
  return b;
}

a = minimoAbsoluto(arreglosTest1, arreglosTest2);

console.log("a=", a);

Another option is concat

var arreglosTest1 = [21, 9, 34],
  arreglosTest2 = [9, 8, 13];

function minimoAbsoluto(...arreglos) {
  var c = [].concat(...arreglos);
  console.log(c);
  var b = Math.min(...c);
  return b;
}

a = minimoAbsoluto(arreglosTest1, arreglosTest2);

console.log("a=", a);

Another option, which even eliminates duplicates, is using sets

var arreglosTest1 = [21, 9, 34],
  arreglosTest2 = [9, 8, 13];

function minimoAbsoluto(...arreglos) {
  var c = [ ...new Set( [].concat(...arreglos) ) ];
  console.log(c);
  var b = Math.min(...c);
  return b;
}

a = minimoAbsoluto(arreglosTest1, arreglosTest2);

console.log("a=", a);
    
answered by 21.07.2018 в 21:46