Questions tagged [rest-parameters]

17 questions
68
votes
11 answers

Spread Syntax vs Rest Parameter in ES2015 / ES6

I am confused about the spread syntax and rest parameter in ES2015. Can anybody explain the difference between them with proper examples?
29
votes
3 answers

What is the meaning of "...args" (three dots) in a function definition?

It was really confusing for me to read this syntax in Javascript: router.route('/:id') .put((...args) => controller.update(...args)) .get((...args) => controller.findById(...args)); What does ...args mean?
Cesar Jr Rodriguez
  • 1,244
  • 3
  • 17
  • 26
18
votes
6 answers

Usage of rest parameter and spread operator in javascript

What's the usage of rest parameter that will be added in ECMAScript 6? For example, in ECMAScript 5 you can do the following to get an array of parameters starting from the second element: // ES 5 store('Joe', 'money'); store('Jane', 'letters',…
Tareq Salah
  • 3,622
  • 4
  • 32
  • 45
17
votes
1 answer

Typed function parameters using destructuring and rest in TypeScript

I have a function: export default ({ input: { name, onChange, value, ...restInput }, meta, ...rest }) => ( ... ); Given that 'name' is a string, 'onChange' is a function, 'value' is a string, 'meta' is an object, how can I add types to…
n3stle
  • 877
  • 1
  • 14
  • 35
15
votes
3 answers

Is It Possible To Set Default Parameter Value On A Rest Parameter

ES6 introduces a bevy of convenient "syntactic sugar". Among them are the default parameter capabilities of JavaScript functions, as well as rest parameters. I'm finding that my console (or devTools) complains (i.e., throws an error) whenever…
IsenrichO
  • 3,193
  • 3
  • 14
  • 30
8
votes
2 answers

Why are the rest parameters in JavaScript called so?

Why are rest parameters in JavaScript called so?
Phi_1.618
  • 181
  • 1
  • 1
  • 6
7
votes
5 answers

javascript es6: use case for destructuring rest parameter

I just saw a code snippet in MDN about destructuring rest parameters like so: function f(...[a, b, c]) { return a + b + c; } f(1) // NaN (b and c are undefined) f(1, 2, 3) // 6 f(1, 2, 3, 4) // 6 (the fourth parameter is not…
user3303864
4
votes
1 answer

JS string destructuring: rest parameter returning inconsistent data

Consider the following examples An old project: const [x, ...y] = "text"; console.log(x) // "t" console.log(y) // "ext" A new project based on CRA: const [x, ...y] = "text"; console.log(x) // "t" console.log(y) // ["e", "x", "t"] I'm not sure why…
2
votes
1 answer

How to specify multiple conditions in an array and call it in an if statement in javascript

I don't know this can be possible or not I want to store all my condition in array and need to call it in if statement const addition = (...numbers) => { let arrayOfTest = [ `${numbers.length === 0}`, `${numbers.some(isNaN)}`, …
Aakash
  • 225
  • 13
1
vote
1 answer

js array remove element by value using destructuring/rest syntax

Seemed quite intuitive to me, but turns out that's not how things work! The goal is to remove the passed element if it exists and return the remainder. I know there's a number of ways of achieving this - including filter: const rest =…
kawerewagaba
  • 527
  • 1
  • 6
  • 17
0
votes
1 answer

How can specify type for ...rest in typescript-react?

const { data, error, ...rest } = useSWR( id ? `/api/v1/blogs/${id}` : null, fetcher ); How can i specify the type for ...rest? const {data,error,...rest} : {data:any,error:Error,"I dont know what to place for ...rest"}
Yilmaz
  • 4,262
  • 6
  • 24
  • 52
0
votes
0 answers

Typing const from rest param in destructured object

I'm trying to explicitly type a const which is created using a rest param in a destructured object. I can see that typescript can infer that it will have all the properties of the type of the source object, less any properties which I create…
sauntimo
  • 1,129
  • 1
  • 14
  • 21
0
votes
4 answers

How can I loop through function arguments?

def grade(*score): for i in range(0, score): if score >= 90: return "A" elif score >=80: return "B" elif score >=70: return "C" elif score >=60: return "D" else: return "F" print(grade(87, 92,…
0
votes
1 answer

Type Guard Typescript not inferring function rest parameters correctly

I have an example of Typescript with type guard and function rest parameters: interface BaseProps { username: string; password: string; } type Props = BaseProps & ( | {isAdmin: true, adminName: string} | {isAdmin: false} ) // Doesn't…
nghiepit
  • 2,560
  • 1
  • 17
  • 18
0
votes
0 answers

Javascript Rest Params missing

const Router = { get: (path, ...rest) => { console.log(rest) } } import router from '../shared/router'; router.get('/tester/123', someMiddleware, 'HomeController#index'); the function shows a f(path) with no rest params?? If I change it to…
GN.
  • 4,948
  • 7
  • 36
  • 70
1
2