Function with array parameter in javascript

# Passing Arrays as Function Arguments

The ability to turn an array into a list of arguments is super handy with the Math functions.

# Example: Find the Largest Number

Let’s say you want to find the largest number using the Math.max() function.

const largest = Math.max(5, 7, 3, 4); console.log(largest); // 7 

But rarely, would you pass in individual values. More likely, you would want to find the maximum element in an array. So the question now is, how do you pass an array of values into a function that accepts individual arguments and NOT an array?

const numbers = [5, 7, 3]; // 🤮 Yuck! Math.max(numbers[0], numbers[1], numbers[2]); // ❌ And this won't work Math.max(numbers); // NaN 

Lucky for us, we can use ES6’s Spread operator!

const numbers = [5, 7, 3]; // 😍 Much Better! Math.max(. numbers); // 7 

What spread is doing here is taking the array element and expanding or unpacking it into a list of arguments for our variadic function.

const numbers = [5, 7, 3]; console.log(. numbers); // 5 7 3 

# Explaining spread in non-dev terms

If you find this spread-ing thing still confusing. Maybe let me try to explain it with Russian nesting dolls

Читайте также:  Adding elements to dictionary python

. So I like to think of the array as Russian nesting dolls. And what spread does is:

  1. It unpacks (spread) the nested dolls into individual dolls.
  2. And now you have all these individual dolls (arguments) to place nicely in your display case (function).

Not sure if this explanation helps? Leave a comment if it does, and I’ll start explaining programming concepts in fun ways like this 😆

# Passing Multiple Arrays as Function Arguments

Another superpower spread has is combining arrays.

const one = [1, 2, 3]; const two = [4, 5, 6]; const merged = [. one, . two]; // [ 1, 2, 3, 4, 5, 6 ] 

So we can use this superpower to pass multiple arrays as function arguments 💪

const one = [1, 2, 3]; const two = [4, 5, 6]; Math.max(. one, . two); // 6 

For those keeners, wondering if you can pass in 3 arrays. Well, you betcha! It’s like the energizer bunny, it keeps going and going and going . (This post is not sponsored by Energizer lol. But that can change, hit me up Energizer. Me want some sponsor money 😂)

const one = [1, 2, 3]; const two = [4, 5, 6]; const three = [2, 100, 2]; Math.max(. one, . two, . three); // 100 

# What is a variadic function?

So you may notice I use the term variadic functions. The computer science folks will have probably heard this term. But for the rest of the cool bees like myself 😝, it may not be so familiar. A variadic function is a function that accepts an infinite or variable number of arguments. And the Math.max() function is one of those variadic function.

Источник

Using an Array as Function Parameter in JavaScript

In Javascript, we often have data stored as arrays, and functions we want to call. Sometimes, the data in our arrays is exactly the data we want to pass to a function. Fortunately, there are ways in Javascript to use arrays as the input values for functions. Let’s look at how to use arrays as function parameters.

How to use arrays as function parameters

When we have a function we want to pass an array to, the most basic way to do it would be like this:

let numbers = [ 1, 2, 3 ] let myFunction = (x, y, z) =>   return x + y + z; >  // Returns 6 let getCalculation = myFunction(numbers[0], numbers[1], numbers[2]); 

Of course, this can be quite annoying, especially when working with functions which have very long lists of properties. As such, Javascript provides us with two ways to use arrays as function parameters in Javascript — apply() and the spread operator.

Passing arrays to functions with the spread operator

The modern, and easiest way to pass an array to a function is to use the spread operator. The spread operator ( . ) is simply added before the array. Using our previous example, it looks like this:

let numbers = [ 1, 2, 3 ] let myFunction = (x, y, z) =>   return x + y + z; >  // Returns 6 let getCalculation = myFunction(. numbers); 

We can also directly add the array into the function, without needing a variable. For example:

let myFunction = (x, y, z) =>   return x + y + z; >  // Returns 6 let getCalculation = myFunction(. [ 1, 2, 3 ]); 

Passing arrays to functions using apply()

The other way to do this, is to use apply() . If you’re unfamiliar with the main use case of apply() , check out my full guide on Javascript’s this object here.

Ultimately, apply() lets us take a function, and pass an array into it. The first attribute of apply() actually refers to the this object we want to use on the function. The second argument is an array of all the parameters for that function. That means you can define the structure of this as well as pass an array into the function itself.

This obviously has some specific benefits when compared to the spread ( . ) operator, so it may be more suitable depending on what you want to achieve. Using our previous example, we can pass an array into our function like so:

let myFunction = (x, y, z) =>   return x + y + z; >  // Returns 6 let getCalculation = myFunction.apply(>, [ 1, 2, 3 ]); 

Here, I am leaving the this object empty, so if we did use this in myFunction , it’d be an empty object — that’s what the first argument of apply() does. The second is our array [1, 2, 3] , referring to x , y , and z respectively.

More Tips and Tricks for Javascript

Источник

# Passing Arrays as Function Arguments

The ability to turn an array into a list of arguments is super handy with the Math functions.

# Example: Find the Largest Number

Let’s say you want to find the largest number using the Math.max() function.

const largest = Math.max(5, 7, 3, 4); console.log(largest); // 7 

But rarely, would you pass in individual values. More likely, you would want to find the maximum element in an array. So the question now is, how do you pass an array of values into a function that accepts individual arguments and NOT an array?

const numbers = [5, 7, 3]; // 🤮 Yuck! Math.max(numbers[0], numbers[1], numbers[2]); // ❌ And this won't work Math.max(numbers); // NaN 

Lucky for us, we can use ES6’s Spread operator!

const numbers = [5, 7, 3]; // 😍 Much Better! Math.max(. numbers); // 7 

What spread is doing here is taking the array element and expanding or unpacking it into a list of arguments for our variadic function.

const numbers = [5, 7, 3]; console.log(. numbers); // 5 7 3 

# Explaining spread in non-dev terms

If you find this spread-ing thing still confusing. Maybe let me try to explain it with Russian nesting dolls

. So I like to think of the array as Russian nesting dolls. And what spread does is:

  1. It unpacks (spread) the nested dolls into individual dolls.
  2. And now you have all these individual dolls (arguments) to place nicely in your display case (function).

Not sure if this explanation helps? Leave a comment if it does, and I’ll start explaining programming concepts in fun ways like this 😆

# Passing Multiple Arrays as Function Arguments

Another superpower spread has is combining arrays.

const one = [1, 2, 3]; const two = [4, 5, 6]; const merged = [. one, . two]; // [ 1, 2, 3, 4, 5, 6 ] 

So we can use this superpower to pass multiple arrays as function arguments 💪

const one = [1, 2, 3]; const two = [4, 5, 6]; Math.max(. one, . two); // 6 

For those keeners, wondering if you can pass in 3 arrays. Well, you betcha! It’s like the energizer bunny, it keeps going and going and going . (This post is not sponsored by Energizer lol. But that can change, hit me up Energizer. Me want some sponsor money 😂)

const one = [1, 2, 3]; const two = [4, 5, 6]; const three = [2, 100, 2]; Math.max(. one, . two, . three); // 100 

# What is a variadic function?

So you may notice I use the term variadic functions. The computer science folks will have probably heard this term. But for the rest of the cool bees like myself 😝, it may not be so familiar. A variadic function is a function that accepts an infinite or variable number of arguments. And the Math.max() function is one of those variadic function.

Источник

Оцените статью