jeandremelaria
4/2/2018 - 2:42 PM

Spread & Rest Operators

// Spread - Used to split up array elements or object properties
const newArray = [...oldArray,1,2];
const newObject = {...oldObject, newProp:5};

//Example
const numbers = [1,2,3];
const newNumbers = [...numbers, 4];
console.log(newNumbers); //gives [1,2,3,4]

const person = {
	name:'Max'
};

const newPerson = {
	...person,
	age:28
}

console.log(newPerson); // gives [object Object] { age:28, name:'Max'}

//Used to merge a list of function arguments into an array
function sortArgs(..args){
	return args.sort();
}

// ...args merges input into an array
const filter = (...args) => {
	return args.filter(el => el === 1);
}

console.log(filter(1,2,3)); // gives [1]