Skip to content

Instantly share code, notes, and snippets.

View NyaGarcia's full-sized avatar
🐈

Nya NyaGarcia

🐈
View GitHub Profile
@NyaGarcia
NyaGarcia / dynamic-destructure-array.js
Last active September 21, 2019 15:39
Dynamically destructuring an array of properties with the spread operator
function dynamicDestructureMany(object, properties) {
return properties.map(property => {
const { [property]: value } = object;
return value;
});
}
console.log(dynamicDestructureMany(pokemon, ['name', 'type'])); //Result: ['Squirtle', 'Water']
@NyaGarcia
NyaGarcia / dynamic-destructure-array-es6.js
Created September 21, 2019 15:40
Dynamically destructuring an array of properties with the spread operator (ES6 Arrow function)
const dynamicDestructureManyES6 = (object, properties) =>
properties.map(property => {
const { [property]: value } = object;
return value;
});
console.log(dynamicDestructureManyES6(pokemon, ['name', 'type'])); //Result: ['Squirtle', 'Water']
@NyaGarcia
NyaGarcia / dynamic-destructure.js
Created September 21, 2019 15:53
Dynamically destructuring object properties with the spread operator
const pokemon = {
id: 1,
name: 'Squirtle',
type: 'Water'
};
//Destructuring a dynamic property
const dynamicProperty = 'name';
const { [dynamicProperty]: value } = pokemon;
@NyaGarcia
NyaGarcia / destructure-defaults.js
Created September 22, 2019 14:30
Using default values when destructuring
const pokemon = {
id: 1,
name: 'Squirtle'
};
const { type, name } = pokemon;
console.log(name); //Result: Squirtle
console.log(type); //Result: undefined
//Assigning default value to the type variable
@NyaGarcia
NyaGarcia / conditional-property-if.js
Created September 22, 2019 14:31
Adding properties conditionally with if statement
const pokemon = {
name: 'Squirtle',
type: 'Water'
};
const abilities = ['Torrent', 'Rain dish'];
const fullPokemon = abilities ? { ...pokemon, abilities } : pokemon;
console.log(fullPokemon);
@NyaGarcia
NyaGarcia / short-circuiting-&&.js
Created September 22, 2019 16:48
Short circuiting with && operator
const starterPokemon = ['Squirtle', 'Charmander', 'Bulbasur'];
function choosePokemon(pokemon) {
return pokemon[Math.floor(Math.random() * pokemon.length)];
}
//Checks if the starterPokemon array has any elements and runs the choosePokemon function
console.log(starterPokemon.length > 0 && choosePokemon(starterPokemon));
//It's equivalent to doing this:
if (starterPokemon.length > 0) {
@NyaGarcia
NyaGarcia / conditional-property-spread.js
Created September 22, 2019 16:57
Adding properties conditionally with spread operator
const pokemon = {
name: 'Squirtle',
type: 'Water'
};
const abilities = ['Torrent', 'Rain dish'];
const fullPokemon = {
...pokemon,
...(abilities && { abilities })
};
@NyaGarcia
NyaGarcia / nested-object-clone.js
Last active September 27, 2019 15:42
Cloning an object with nested array, with the spread operator
const pokemon = {
name: 'Squirtle',
type: 'Water',
abilities: ['Torrent', 'Rain Dish']
};
const squirtleClone = { ...pokemon, abilities: [...pokemon.abilities] };
pokemon.name = 'Charmander';
pokemon.abilities.push('Surf');
@NyaGarcia
NyaGarcia / default-property.js
Last active December 22, 2021 17:38
Adding a default property with the spread operator
const pokemon = {
name: 'Squirtle',
type: 'Water'
};
const { abilities = [], ...rest } = pokemon;
const fullSquirtle = { ...rest, abilities };
console.log(rest); //Result: { name: 'Squirtle', type: 'Water' }
console.log({ fullSquirtle }); //Result: { name: 'Squirtle', type: 'Water', abilities: [] }
@NyaGarcia
NyaGarcia / rest-parameter.js
Created September 23, 2019 15:41
An example showcasing the use of the rest parameter
function printPokemon(name, type, ...abilities) {
console.log(
`${name} is a ${type} type Pokemon. ${name}'s abilities are: ${abilities.join(
', '
)}`
);
}
printPokemon('Squirtle', 'Water', 'Torrent');
//Squirtle is a Water type Pokemon. Squirtle's abilities are: Torrent