Skip to content

Instantly share code, notes, and snippets.

@sorie
Created August 12, 2021 01:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sorie/03c34297df8f0e717ec27fea2bba28c0 to your computer and use it in GitHub Desktop.
Save sorie/03c34297df8f0e717ec27fea2bba28c0 to your computer and use it in GitHub Desktop.
javascript tip : Spread Syntax - Object,array
//Spread Syntax - Object
const item = { type: 'shirts', size : 'M' };
const detail = { price: 20, made: 'Kores', gender: 'M' };
//bad code
item['price'] = detail.price;
//bad code
const newObject = new Object;
newObject['type'] = itme.type;
newObject['size'] = itme.size;
newObject['price'] = itme.price;
newObject['made'] = itme.made;
newObject['gender'] = itme.gender;
//bad code
const newObject2 = {
type: item.type,
size: item.size,
price: detail.price,
made: detail.made,
gender: detail.gender,
};
//good code
const chirt0 == Object.assign(item, detail);
//better code
const shirt = {...item, ...detail, price: 40 };
//spread syntax - arrray
let fruits = ['수박', '귤', '바나나'];
//fruits.push
fruits = [...fruits, '딸기'];
//fruits.unshift
fruits = ['딸기', ...fruits];
const fruits2 = ['메론','피치', '파인애플'];
let combined = fruits.concat(fruits2);
combined = [...fruits, ...fruits2);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment