- 前言
- Lazy
- createWrapper
- generate
- range
- repeat
- strict
- Sequence
- define
- async
- chunk
- compact
- concat
- consecutive
- contains
- countBy
- dropWhile
- each
- equals
- every
- filter
- find
- findWhere
- first
- flatten
- get
- getIterator
- groupBy
- indexBy
- indexOf
- initial
- intersection
- invoke
- isEmpty
- join
- last
- lastIndexOf
- map
- max
- memoize
- min
- none
- ofType
- pluck
- reduce
- reduceRight
- reject
- rest
- reverse
- shuffle
- size
- some
- sort
- sortBy
- sortedIndex
- sum
- takeWhile
- tap
- toArray
- toObject
- union
- uniq
- where
- without
- zip
- ArrayLikeSequence
- define
- concat
- first
- get
- length
- map
- pop
- push
- rest
- reverse
- shift
- slice
- unshift
- ObjectLikeSequence
- define
- assign
- async
- defaults
- functions
- get
- invert
- keys
- merge
- omit
- pairs
- pick
- toArray
- toObject
- values
- watch
- StringLikeSequence
- define
- charAt
- charCodeAt
- contains
- endsWith
- first
- indexOf
- last
- lastIndexOf
- mapString
- match
- reverse
- split
- startsWith
- substring
- toLowerCase
- toUpperCase
- GeneratedSequence
- each
- length
- AsyncSequence
- contains
- each
- find
- getIterator
- indexOf
- Iterator
- current
- moveNext
- AsyncHandle
- cancel
- onComplete
- onError
merge
Produces an ObjectLikeSequence consisting of all the recursively merged values from this and the given object(s) or sequence(s).
Note that by default this method only merges "vanilla" objects (bags of key/value pairs), not arrays or any other custom object types. To customize how merging works, you can provide the mergeFn argument, e.g. to handling merging arrays, strings, or other types of objects.
Signature
ObjectLikeSequence.merge = function(others, mergeFn) { /*...*/ }
ObjectLikeSequence.merge = function merge(var_args) { var mergeFn = arguments.length > 1 && typeof arguments[arguments.length - 1] === "function" ? arrayPop.call(arguments) : null; return new MergedSequence(this, arraySlice.call(arguments, 0), mergeFn); }
Name | Type(s) | Description |
---|---|---|
others | ...Object|ObjectLikeSequence | The other object(s) or sequence(s) whose values will be merged into this one. |
mergeFn | Function? | An optional function used to customize merging behavior. The function should take two values as parameters and return whatever the "merged" form of those values is. If the function returns undefined then the new value will simply replace the old one in the final result. |
returns | ObjectLikeSequence | The new sequence consisting of merged values. |
Examples
// These examples are completely stolen from Lo-Dash's documentation: // lodash.com/docs#merge var names = { 'characters': [ { 'name': 'barney' }, { 'name': 'fred' } ] }; var ages = { 'characters': [ { 'age': 36 }, { 'age': 40 } ] }; var food = { 'fruits': ['apple'], 'vegetables': ['beet'] }; var otherFood = { 'fruits': ['banana'], 'vegetables': ['carrot'] }; function mergeArrays(a, b) { return Array.isArray(a) ? a.concat(b) : undefined; } Lazy(names).merge(ages); // => sequence: { 'characters': [{ 'name': 'barney', 'age': 36 }, { 'name': 'fred', 'age': 40 }] } Lazy(food).merge(otherFood, mergeArrays); // => sequence: { 'fruits': ['apple', 'banana'], 'vegetables': ['beet', 'carrot'] } // ----- Now for my own tests: ----- // merges objects Lazy({ foo: 1 }).merge({ foo: 2 }); // => sequence: { foo: 2 } Lazy({ foo: 1 }).merge({ bar: 2 }); // => sequence: { foo: 1, bar: 2 } // goes deep Lazy({ foo: { bar: 1 } }).merge({ foo: { bar: 2 } }); // => sequence: { foo: { bar: 2 } } Lazy({ foo: { bar: 1 } }).merge({ foo: { baz: 2 } }); // => sequence: { foo: { bar: 1, baz: 2 } } Lazy({ foo: { bar: 1 } }).merge({ foo: { baz: 2 } }); // => sequence: { foo: { bar: 1, baz: 2 } } // gives precedence to later sources Lazy({ foo: 1 }).merge({ bar: 2 }, { bar: 3 }); // => sequence: { foo: 1, bar: 3 } // undefined gets passed over Lazy({ foo: 1 }).merge({ foo: undefined }); // => sequence: { foo: 1 } // null doesn't get passed over Lazy({ foo: 1 }).merge({ foo: null }); // => sequence: { foo: null } // array contents get merged as well Lazy({ foo: [{ bar: 1 }] }).merge({ foo: [{ baz: 2 }] }); // => sequence: { foo: [{ bar: 1, baz: 2}] }
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论