“Parametr REST” Kod odpowiedzi

operator reszty JavaScript

function sum(...numbers) {
	return numbers.reduce((accumulator, current) => {
		return accumulator += current;
	});
};
 
sum(1,2) // 3
sum(1,2,3,4,5) // 15
Brainy Butterfly

Parametry rozprzestrzeniania JS

// seperate each element of array using ...
let list = ['a','b','c'];
let copy = [...list, 'd', 'e']; // ['a', 'b', 'c', 'd', 'e']
//use for infinite parameters to a function
function toDoList(...todos) {
  //todos is an array, so it has map function
  document.write(
    `<ul>${todos.map((todo) => `<li>${todo}</li>`).join("")}</ul>`
  );
}
toDoList("wake up", "eat breakfast", ...list); //ul containing: wake up eat breakfast a b c
Google

Parametr REST

//******************************************rest parameter
 const double = (...nums) => {//rest parameter is initialized by using three dots
//The numbers in the array called "result" is passed into the rest parameter. 

  console.log(nums);//each number in the rest parameter is ouput to the console
  return nums.map(num => num*2);//each number in the rest parameter is multiplied by 2 and then stored inside a map
};

const result = double(1,3,5,7,2,4,6,8);//creates a constant,called  "result". It then holds an array of numbers, in this example, and it could be an array of any length. 
//The numbers in parentheses after "double" are passed into the rest parameter also called "double"
console.log(result);//The final result is displayed, with each number having been multiplied by 2





// ************************************spread syntax (arrays)
//The spread syntax is similar to the 'rest parameter', one still uses the three dots to initialize it, but the syntax differs slightly, breaking the array up into its various components
const people = ['shaun', 'ryu', 'chun-li'];
//a good use for the spread array would be when we want to take the elements of one array, and spread them into another array (combine them)
const members = ['mario', 'luigi', ...people];
console.log(members);

//spread syntax (objects)
//again, it is similar to the spread syntax for arrays, but this time it is being applied to objects
const person = { name: 'shaun', age: 30, job: 'net ninja' };
//A NEW OBJECT is created, which copies the OBJECT called 'person' and then ALSO creates and fuses the 'personClone' array into the same object
const personClone = { ...person, location: 'manchester' };
console.log(person, personClone);
Meandering Meerkat

Składnia parametrów REST

foo(arg1, arg2, ...correct)
marwa benhssine

Parametry odpoczynku

const arr = [1, 2, 3];

let [x, ...z] =arr;
console.log(z);
console.log(x);
Raymund Alum

Odpowiedzi podobne do “Parametr REST”

Pytania podobne do “Parametr REST”

Więcej pokrewnych odpowiedzi na “Parametr REST” w JavaScript

Przeglądaj popularne odpowiedzi na kod według języka

Przeglądaj inne języki kodu