Created
August 12, 2021 01:10
-
-
Save sorie/03c34297df8f0e717ec27fea2bba28c0 to your computer and use it in GitHub Desktop.
javascript tip : Spread Syntax - Object,array
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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