返回介绍

merge

发布于 2019-05-31 13:13:07 字数 3788 浏览 1049 评论 0 收藏 0

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);
}
NameType(s)Description
others...Object|ObjectLikeSequence

The other object(s) or sequence(s) whose values will be merged into this one.

mergeFnFunction?

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.

returnsObjectLikeSequence

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文