[Javascript] Combine Objects with Object.assign and Lodash merge
JavaScript and with object Merge Objects assign Combine
2023-09-14 09:00:52 时间
Learn how to use Object.assign to combine multiple objects together. This pattern is helpful when writing libraries that have a set of default options where you want to allow a user to override specific options. You'll also learn how to use a third party library to recursively merge nested objects together.
const defaultOptions = { amount: 10, quantity: 1, color: 'maroon', material: 'cotton', sizes: { small: true, medium: true, large: true } }; function inventory(userOptions) { console.log(Object.assign({}, defaultOptions, userOptions)); /* [object Object] { amount: 15, color: "maroon", material: "wool", quantity: 6, sizes: [object Object] { extraLarge: true } } */ console.log(_.merge({}, defaultOptions, userOptions)); /* [object Object] { amount: 15, color: "maroon", material: "wool", quantity: 6, sizes: [object Object] { extraLarge: true, large: true, medium: true, small: true } } */ }; inventory({ amount: 15, quantity: 6, material: 'wool', sizes: { extraLarge: true } });
相关文章
- 怎样用JavaScript和HTML5 Canvas绘制图表
- JavaScript中的Function(函数)对象详解
- Windows Store App JavaScript 开发:简单对象绑定
- [Javascript] Objects and Functions
- [Javascript] Ex: concatAll, map and filter
- [Javascript] Drawing Paths - Lines and Rectangles
- [AngularJS] Javascript scope and AngularJS $scope
- [Javascript] Private, public variables and method in Name Space
- [Javascript] Prototype, hasOwnProperty(), valueOf() and toString() methods.
- 【JavaScript】underscore
- [Javascript] Create an Async Generator and Loop Through Generated Promises with "For Await Of" Loops
- [ES2019] Use JavaScript ES2019 flatMap to Map and Filter an Array
- [Javascript] Format console.log with CSS and String Template Tags
- [Javascript] Log Levels and Semantic Methods
- [Javascript] Adding Shapes to Maps with Leaflet and GeoJSON
- javascript 显示一定范围内的素数(质数)
- Javascript 学习 笔记二
- 如何使用Javascript复制到剪贴板
- 浅谈JavaScript中的原型模式
- Prototype and Constructor in JavaScript
- Javascript 中 的 for ... in 和 for ... of 差别