JavaScript How to pass a spread operator to a functions parameter pass props spread operator,react pass props spread operator,passing

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.

Answer:1

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);
Answer:2

I am writing some tests for my React component: let DebugElement = TestUtils.renderIntoDocument(<Debug />); console.log(DebugElement) spyOn(DebugElement, 'fetchAndSelect'); However, it says ...

I am writing some tests for my React component: let DebugElement = TestUtils.renderIntoDocument(<Debug />); console.log(DebugElement) spyOn(DebugElement, 'fetchAndSelect'); However, it says ...

  1. access instance methods
  2. access instance method java
  3. access instance method php
  4. access instance method in class
  5. call instance methods from constructor
  6. call instance methods ruby
  7. call instance methods java
  8. call instance methods
  9. call instance method python
  10. call instance method abap
  11. call instance method objective c
  12. call instance method in sap abap
  13. call instance method from class abap
  14. call instance method php
  15. call instance method sap
  16. call instance method in swift
  17. call instance method c#
  18. call instance method from class
  19. call instance method in abap 7.4
  20. call instance method module

I don't get it why the second console.log returns a null while the first console returns 1 Code Source var digit = /\d/g; console.log(digit.exec("here it is: 1")); console.log(digit.exec("...

I don't get it why the second console.log returns a null while the first console returns 1 Code Source var digit = /\d/g; console.log(digit.exec("here it is: 1")); console.log(digit.exec("...

In the Jangaroo tutorial using Maven it states"include a Jangaroo application script generated by the Maven build process". This should be created in src/main/webapp/index.html, it isn't. Can anyone ...

In the Jangaroo tutorial using Maven it states"include a Jangaroo application script generated by the Maven build process". This should be created in src/main/webapp/index.html, it isn't. Can anyone ...

I am trying to have a change method for all visible select elements. $('.select_elements:visible').change(function() { // function )}; The above doesn't seem to run when I change the select ...

I am trying to have a change method for all visible select elements. $('.select_elements:visible').change(function() { // function )}; The above doesn't seem to run when I change the select ...

  1. change method in jquery
  2. change method in angular 6
  3. change method to static java
  4. change method in javascript
  5. change method to
  6. change payment method with family sharing
  7. change payment method with uber
  8. java change method with reflection
  9. change payment method with apple
  10. change payment method with itunes
  11. change payment method with apple id
  12. change payment method with paypal
  13. change in method of depreciation
  14. change in method of depreciation questions and answers
  15. change in method of depreciation as per as 10
  16. change in method of depreciation questions and answers pdf
  17. change in method of accounting
  18. change in method of depreciation questions
  19. change in method of depreciation as per ind as
  20. change in method of valuation of inventory