2

Using spread operator I can get the maximum value of two arrays using Math.max(...firstArr, ...secondArr), and get's the right value, I am trying not to add a spread operator MANUALLY, I am trying to add automatically by creating function, I am trying to add or pass a spread operator before the argument. example

var firstArr = [1,2,3,4,5];
var secondArr = [6,7,8,9];

function myFun() {
    for(var i = 0; i <= arguments.length; i++) {
      var resl = Math.max(...arguments[i]);
      document.write("The maximum value is " + resl);
    }
}

myFun(firstArr, secondArr);

It displays two result values:

The first result will be: "The maximum value is 5"

The second result will be: "The maximum value is 9"

It is not comparing two arrays at a time, Please help me. Thanks.

Rasool
  • 173
  • 2
  • 10
  • 1
    I don't get what you mean by "*I am trying to add automatically by creating function, I am trying to add or pass a spread operator before the argument.*" Notice that spread is syntax, not an operator, and you cannot "add" it arbitrarily. – Bergi Feb 17 '17 at 02:02
  • 2
    What do you mean by "*It is not comparing two arrays at a time*"? What is the expected result that you would like to get from which call? – Bergi Feb 17 '17 at 02:03
  • 1
    It's `i < arguments.length` not `i <= arguments.length` – Bergi Feb 17 '17 at 02:03

3 Answers3

3

You seem to be saying that you want your function to take a variable number of separate arrays as arguments, and then find the maximum number within any of those arrays.

If so, you can say [].concat(...arguments) to create a single new array with all of the values from the individual arrays that were arguments, then use the spread operator to pass that new array to Math.max(). (You don't need a loop.)

var firstArr = [1,2,3,4,5];
var secondArr = [6,7,8,9];

function myFun() {
    var resl = Math.max(...[].concat(...arguments));
    console.log("The maximum value is " + resl);
}

myFun(firstArr, secondArr);
nnnnnn
  • 147,572
  • 30
  • 200
  • 241
3

It sounds like what you are trying to do is

function myFun(...arrays) {
    const allValues = [].concat(...arrays);
    return Math.max(...allValues);
}
console.log("The maximum value is " + myFun([1,2,3,4,5], [6,7,8,9]));

However I would recommend to avoid spread syntax with potentially large data, and go for

function myFun(...arrays) {
    return Math.max(...arrays.map(arr => Math.max(...arr)));
}

or even better

function myFun(...arrays) {
    return arrays.map(arr => arr.reduce(Math.max)).reduce(Math.max);
}
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
  • Which of your map and reduce options is faster? (It looks to me like the first would have far fewer calls to `Math.max()`, but I'm not sure if that matters.) – nnnnnn Feb 17 '17 at 02:20
  • @nnnnnn I didn't care so much about speed as about [still working with large arrays](http://stackoverflow.com/q/42263108/1048572) – Bergi Feb 17 '17 at 02:24
  • @Bergi Thank you so much for your reply it really helps, thanks again ... :) – Rasool Feb 17 '17 at 02:39
  • Oh. I guess you did say "potentially large data", but it didn't occur to me that that meant "potentially large number of arguments". I can see why the reduce option would help with that, thanks. – nnnnnn Feb 17 '17 at 02:49
0

You can use rest element at function declaration, pass an array of arrays each preceded by spread element to function parameters and spread element within function

function myFun(...arr) {
  return Math.max.apply(Math, ...arr)
}

myFun([...firstArr, ...secondArr /*, ...nArr*/ ]);
guest271314
  • 1
  • 15
  • 104
  • 177