最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to pass a spread operator to a functions parameter - Stack Overflow

programmeradmin4浏览0评论

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 paring two arrays at a time, Please help me. Thanks.

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 paring two arrays at a time, Please help me. Thanks.

Share Improve this question asked Feb 17, 2017 at 2:00 RasoolRasool 1833 silver badges13 bronze badges 3
  • 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 Commented Feb 17, 2017 at 2:02
  • 2 What do you mean by "It is not paring two arrays at a time"? What is the expected result that you would like to get from which call? – Bergi Commented Feb 17, 2017 at 2:03
  • 1 It's i < arguments.length not i <= arguments.length – Bergi Commented Feb 17, 2017 at 2:03
Add a ment  | 

3 Answers 3

Reset to default 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);

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 remend 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);
}

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*/ ]);
发布评论

评论列表(0)

  1. 暂无评论