javascript Lodash:从多个构建单个对象 - 合并/覆盖属性
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21120276/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Lodash: Constructing single object from many - Merging/overriding properties
提问by Olivier Clément
Note: I filed this question under lodash as I'm pretty sure it can help me solve that problem nicely, but haven't put my finger on it just now
注意:我在 lodash 下提交了这个问题,因为我很确定它可以帮助我很好地解决这个问题,但现在还没有解决
I have an object describing different user roles and their permissions;
我有一个描述不同用户角色及其权限的对象;
I will have something like 10-15 roles defined "like this" (this doesn't reflect the application code but the problem itself):
我将“像这样”定义 10-15 个角色(这不反映应用程序代码,而是问题本身):
var role1 = {
views: {
v1: {access: true},
v2: {access: false},
v#: {access: false}
}
}
var role2 = {
views: {
v1: {access: false},
v2: {access: true},
v3: {access: true},
}
}
The user connected will have multiple roles; In that example it could be ['role1', 'role2']
, and out of this I need to construct a single permissions
object that will be a combination of all the props defined in all the user roles.
连接的用户将有多个角色;在那个例子中,它可能是['role1', 'role2']
,除此之外,我需要构造一个单一的permissions
对象,它将是所有用户角色中定义的所有道具的组合。
It is basically whitelist-based, where all "true" properties should override anything that was defined as false. Thus, the expected result should be:
它基本上是基于白名单的,其中所有“true”属性都应该覆盖任何定义为 false 的属性。因此,预期的结果应该是:
permissions = {
views: {
v1: {access: true},
v2: {access: true},
v2: {access: true}
}
}
I'm not too sure how to tackle that one without relying on crazy nested loops
我不太确定如何在不依赖疯狂嵌套循环的情况下解决这个问题
Here's a starting point in JSBin: http://jsbin.com/usaQejOJ/1/edit?js,console
这是 JSBin 的起点:http://jsbin.com/usaQejOJ/1/edit?js,console
Thanks for your help!
谢谢你的帮助!
回答by adamesque
Lodash has a few methods that will help to elegantly solve this problem.
Lodash 有一些方法可以帮助优雅地解决这个问题。
First of all, the merge
method. It takes multiple source objects, and recursively merges their properties together into a destination object. If two objects have the same property name, the latter value will override the former.
首先是merge
方法。它接受多个源对象,并递归地将它们的属性合并到一个目标对象中。如果两个对象具有相同的属性名称,则后者的值将覆盖前者。
This is almost what we want; it'll merge your role objects together into a single object. What we don't want is that override behavior; we want true
values to always override false
ones. Luckily, you can pass in a custom merge function, and lodash's merge
will use that to compute the merged value when two objects have the same key.
这几乎就是我们想要的;它会将您的角色对象合并为一个对象。我们不想要的是覆盖行为;我们希望true
值总是覆盖false
那些。幸运的是,您可以传入一个自定义合并函数,merge
当两个对象具有相同的键时,lodash将使用它来计算合并值。
We'll write a custom function to logically OR
your items together (instead of allowing later false
values to override true
s.), so that if either value is true
, the resulting merged value will be true
.
我们将编写一个自定义函数,将OR
您的项目逻辑上放在一起(而不是允许后面的false
值覆盖true
s。),以便如果任一值是true
,则生成的合并值将是true
。
Because our objects are nested, we'll need to make sure our custom merge function only does this OR
when it's comparing boolean values. When comparing objects, we want to do a normal merge of their properties (again using our custom function to do the merge). It looks something like this:
因为我们的对象是嵌套的,所以我们需要确保我们的自定义合并函数只OR
在比较布尔值时才这样做。比较对象时,我们希望对其属性进行正常合并(再次使用我们的自定义函数进行合并)。它看起来像这样:
function do_merge(roles) {
// Custom merge function ORs together non-object values, recursively
// calls itself on Objects.
var merger = function (a, b) {
if (_.isObject(a)) {
return _.merge({}, a, b, merger);
} else {
return a || b;
}
};
// Allow roles to be passed to _.merge as an array of arbitrary length
var args = _.flatten([{}, roles, merger]);
return _.merge.apply(_, args);
}
do_merge([role1, role2, role3]);
Lodash helps out in one other way: You may notice from the docs that _.merge
doesn't accept an array of objects to merge together; you have to pass them in as arguments. In our case, though, an array would be awfully convenient.
Lodash 以另一种方式提供帮助:您可能会从文档中注意到_.merge
不接受将对象数组合并在一起;你必须将它们作为参数传递。但是,在我们的例子中,数组会非常方便。
To get around this, we'll use JavaScript's apply
method, which allows you to invoke a method by passing its arguments as an array, and Lodash's handy flatten
method, which takes an array that might contain nested arrays — like [1, [2, 3], [4, 5]]
—?and flattens it into [1, 2, 3, 4, 5]
.
为了解决这个问题,我们将使用 JavaScript 的apply
方法,它允许您通过将其参数作为数组传递来调用方法,以及 Lodash 的方便的flatten
方法,它接受一个可能包含嵌套数组的数组 - 例如[1, [2, 3], [4, 5]]
-? 并将其展平为[1, 2, 3, 4, 5]
.
So we'll gather up all the arguments we need in a flatten
ed array, and then apply
them to merge
!
因此,我们将在flatten
ed 数组中收集我们需要的所有参数,然后将apply
它们添加到merge
!
If your objects are nested very very deeply, there's a chance you'll overflow the stack calling merger
recursively like this, but for your objects this should work just fine.
如果您的对象嵌套非常深,您可能会merger
像这样递归调用堆栈溢出,但是对于您的对象,这应该可以正常工作。