filmov
tv
Mastering the Spread Operator in JavaScript

Показать описание
// # shallow copy of an array.
// # Using Spread with Arrays
// # Copying Arrays
// # Concatenating Arrays
// # Spreading Array Elements in Function Calls
// # Using Spread with Objects
// # Copying Objects
// # Merging Objects
// # Spreading Object Properties in Function Calls
// # Use Cases and Benefits
// # Immutable Data Manipulation
// # Function Parameter Spreading
// # Array/Object Destructuring
// Copying Arrays
const originalArray = [1, 2, 3];
const copiedArray = [...originalArray];
// Concatenating Arrays
const array1 = [1, 2];
const array2 = [3, 4];
const concatenatedArray = [...array1, ...array2];
// Spreading Array Elements in Function Calls
function sum(a, b, c) {
return a + b + c;
}
const numbers = [1, 2, 3];
const result = sum(...numbers);
// Copying Objects
const originalObj = { x: 1, y: 2 };
const copiedObj = { ...originalObj };
// Merging Objects
const obj1 = { x: 1, y: 2 };
const obj2 = { y: 3, z: 4 };
const mergedObj = { ...obj1, ...obj2 };
// Spreading Object Properties in Function Calls
function greet({ firstName, lastName }) {
}
const person = { firstName: "John", lastName: "Doe" };
greet({ ...person }); // Output: Hello, John Doe!
// # Using Spread with Arrays
// # Copying Arrays
// # Concatenating Arrays
// # Spreading Array Elements in Function Calls
// # Using Spread with Objects
// # Copying Objects
// # Merging Objects
// # Spreading Object Properties in Function Calls
// # Use Cases and Benefits
// # Immutable Data Manipulation
// # Function Parameter Spreading
// # Array/Object Destructuring
// Copying Arrays
const originalArray = [1, 2, 3];
const copiedArray = [...originalArray];
// Concatenating Arrays
const array1 = [1, 2];
const array2 = [3, 4];
const concatenatedArray = [...array1, ...array2];
// Spreading Array Elements in Function Calls
function sum(a, b, c) {
return a + b + c;
}
const numbers = [1, 2, 3];
const result = sum(...numbers);
// Copying Objects
const originalObj = { x: 1, y: 2 };
const copiedObj = { ...originalObj };
// Merging Objects
const obj1 = { x: 1, y: 2 };
const obj2 = { y: 3, z: 4 };
const mergedObj = { ...obj1, ...obj2 };
// Spreading Object Properties in Function Calls
function greet({ firstName, lastName }) {
}
const person = { firstName: "John", lastName: "Doe" };
greet({ ...person }); // Output: Hello, John Doe!