Skip to content

Instantly share code, notes, and snippets.

View Mohamed-Code-309's full-sized avatar
🎯
Focusing

Mohamed Jebril Mohamed-Code-309

🎯
Focusing
View GitHub Profile
@NyaGarcia
NyaGarcia / merge-arrays.js
Last active December 20, 2021 13:44
Merging arrays with the spread operator
const pokemon = ['Squirtle', 'Bulbasur', 'Charmander'];
const morePokemon = ['Totodile', 'Chikorita', 'Cyndaquil'];
const pokedex = [...pokemon, ...morePokemon];
console.log(pokedex); //Result: [ 'Squirtle', 'Bulbasur', 'Charmander', 'Totodile', 'Chikorita', 'Cyndaquil' ]
@NyaGarcia
NyaGarcia / merge-object-arrays.js
Created September 12, 2019 11:47
Merging object arrays with spread operator
const pokemon = [
{ name: 'Squirtle', type: 'Water' },
{ name: 'Bulbasur', type: 'Plant' }];
const morePokemon = [{ name: 'Charmander', type: 'Fire' }];
const pokedex = [...pokemon, ...morePokemon];
console.log(pokedex); //Result: [ { name: 'Squirtle', type: 'Water' }, { name: 'Bulbasur', type: 'Plant' }, { name: 'Charmander', type: 'Fire' } ]
@NyaGarcia
NyaGarcia / add-array-element.js
Last active December 20, 2021 13:44
Adding an element to an array with the spread operator
const pokemon = ['Squirtle', 'Bulbasur'];
const charmander = 'Charmander';
const cyndaquil = 'Cyndaquil';
const pokedex = [...pokemon, charmander, cyndaquil];
console.log(pokedex); //Result: [ 'Squirtle', 'Bulbasur', 'Charmander', 'Cyndaquil' ]
@NyaGarcia
NyaGarcia / spread-array-examples.js
Created September 12, 2019 12:07
Three examples of how the spread operator works with arrays
const numbers = [1, 2, 3];
console.log(...numbers); //Result: 1 2 3
const pokemon = ['Squirtle', 'Bulbasur', 'Charmander'];
console.log(...pokemon); //Squirtle Bulbasur Charmander
const pokedex = [
{ name: 'Squirtle', type: 'Water' },
{ name: 'Bulbasur', type: 'Plant' },
{ name: 'Charmander', type: 'Fire' }
@NyaGarcia
NyaGarcia / add-object-property.js
Last active December 20, 2021 13:44
Adding object properties with the spread operator
const basicSquirtle = { name: 'Squirtle', type: 'Water' };
const fullSquirtle = {
...basicSquirtle,
species: 'Tiny Turtle Pokemon',
evolution: 'Wartortle'
};
console.log(fullSquirtle); //Result: { name: 'Squirtle', type: 'Water', species: 'Tiny Turtle Pokemon', evolution: 'Wartortle' }
@NyaGarcia
NyaGarcia / object-mutation.js
Last active December 20, 2021 13:43
An example of object mutation in JS
const mySquirtle = {
name: 'Squirtle',
type: 'Water',
hp: 100
};
const anotherSquirtle = mySquirtle;
anotherSquirtle.hp = 0;
console.log(mySquirtle); //Result: { name: 'Squirtle', type: 'Water', hp: 0 }
@NyaGarcia
NyaGarcia / immutable-object.js
Created September 14, 2019 15:18
Copying an object immutably with the spread operator
const mySquirtle = {
name: 'Squirtle',
type: 'Water',
hp: 100
};
const anotherSquirtle = { ...mySquirtle };
anotherSquirtle.hp = 0;
console.log(anotherSquirtle); //Result: { name: 'Squirtle', type: 'Water', hp: 0 }
@NyaGarcia
NyaGarcia / immutable-array.js
Created September 14, 2019 15:27
Copying an array immutably with the spread operator
const pokemon = ['Squirtle', 'Bulbasur', 'Charmander'];
const pokedex = [...pokemon];
pokedex.push('Cyndaquil');
console.log(pokemon); //[ 'Squirtle', 'Bulbasur', 'Charmander' ]
console.log(pokedex); //[ 'Squirtle', 'Bulbasur', 'Charmander', 'Cyndaquil' ]
@NyaGarcia
NyaGarcia / merge-objects.js
Created September 14, 2019 15:58
Merging objects with the spread operator
const baseSquirtle = {
name: 'Squirtle',
type: 'Water'
};
const squirtleDetails = {
species: 'Tiny Turtle Pokemon',
evolution: 'Wartortle'
};
@NyaGarcia
NyaGarcia / nodeList-to-array.js
Created September 14, 2019 16:25
Converting a nodeList to array with the spread operator
const nodeList = document.getElementsByClassName("pokemon");
const array = [...nodeList];
console.log(nodeList); //Result: HTMLCollection [ div.pokemon, div.pokemon ]
console.log(array); //Result: Array [ div.pokemon, div.pokemon ]