Last active
October 10, 2020 11:52
-
-
Save YakovSPb/6dfca6f1e7aae5509505ccfed69a1e42 to your computer and use it in GitHub Desktop.
JS как объединить свойства двух объектов
This file contains hidden or 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
JS как объединить свойства двух объектов | |
================== | |
1. spread | |
const obj1 = { | |
food: 'pizza', | |
beverage: 'orange fresh', | |
coffe: 'late' | |
} | |
const obj2 = { | |
salad: 'greek', | |
main: 'grilled chicken', | |
coffe: 'americano' | |
} | |
let merge = {...obj1, ...obj2}; | |
2. Object.assign() | |
const obj1 = { | |
food: 'pizza', | |
beverage: 'orange fresh', | |
coffe: 'late' | |
} | |
const obj2 = { | |
salad: 'greek', | |
main: 'grilled chicken', | |
coffe: 'americano' | |
} | |
let merged2 = Object.assign({}, obj1, obj2); | |
3. цикл for | |
const obj1 = { | |
food: 'pizza', | |
beverage: 'orange fresh', | |
coffe: 'late' | |
} | |
const obj2 = { | |
salad: 'greek', | |
main: 'grilled chicken', | |
coffe: 'americano' | |
} | |
for (let attr in obj1) { merged3[attr] = obj1[attr]; } | |
for (let attr in obj2) { [attr] = obj2[attr]; } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment