Skip to content

Instantly share code, notes, and snippets.

@pjcodesjs
Created May 11, 2021 18:55
Show Gist options
  • Save pjcodesjs/55bf23930b84c870e993ba2620ae4377 to your computer and use it in GitHub Desktop.
Save pjcodesjs/55bf23930b84c870e993ba2620ae4377 to your computer and use it in GitHub Desktop.
//------------------------------------------------
// COPY ELEMENTS FROM ONE ARRAY INTO ANOTHER ARRAY
//------------------------------------------------
const fruits = ['apple', 'mangoe', 'papaya'];
const updated_fruits = ['banana', ...fruits];
console.log(updated_fruits); // [ 'banana', 'apple', 'mangoe', 'papaya' ]
//-------------------
// MERGING TWO ARRAYS
//-------------------
const my_cats = ['Jimmy', 'Marty', 'Max'];
const your_cats = ['George', 'Botsoe', 'Benny'];
const our_cats = [...my_cats, ...your_cats];
console.log(our_cats); // [ 'Jimmy', 'Marty', 'Max', 'George', 'Botsoe', 'Benny' ]
//----------------
// MERGING OBJECTS
//----------------
const profile = {
name: 'Dean Winchester',
age: 34,
car: 'Chevy Imala (Baby)'
}
const list_of_enemies = {
enemy_demons: ['Abadon', 'Yellow Eyes', 'Crowley'],
enemy_angels: ['Zacharia', 'Michael', 'Lucifer']
}
const character = {
...profile,
...list_of_enemies
}
console.log(character);
/*
{
name: 'Dean Winchester',
age: 34,
car: 'Chevy Imala (Baby)',
enemy_demons: [ 'Abadon', 'Yellow Eyes', 'Crowley' ],
enemy_angels: [ 'Zacharia', 'Michael', 'Lucifer' ]
}
*/
@pjcodesjs
Copy link
Author

Spread Operator Basics.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment