-1

How can I pass more than one arg in a function without knowing the number of args that I will need to pass?

function uniteUnique(arr) {
(do somthing here)
}

uniteUnique([1, 3, 2], [5, 2, 1, 4], [2, 1]);

what I mean is the func above receive one arg, but the function is called with 3 arg this time, but in other exercise is called with 4 arg.

The best solution in my case was using arguments

function uniteUnique( ){
console.log(arguments)
}
uniteUnique([1, 3, 2], [5, 2, 1, 4], [2, 1]);
// logs { '0': [ 1, 3, 2 ], '1': [ 5, 2, 1, 4 ], '2': [ 2, 1 ] }
// i can work with this
Chris H.
  • 16
  • 3

1 Answers1

0

This is the way to go if you use typescript/ ES6

function uniteUnique(...arrays) {
(do somthing here)
}

uniteUnique([1, 3, 2], [5, 2, 1, 4], [2, 1]);

where arrays mean variable number of arrays ( could be 2 or 3 or whatever )

or for old javascript / ES5 :

function uniteUnique() {
    (do somthing here , with usage of **`arguments`** object)
    }
    
    uniteUnique([1, 3, 2], [5, 2, 1, 4], [2, 1]);
Helix112
  • 305
  • 3
  • 12