按路径生成嵌套对象
我想制作函数,例如
{ 'person.data.info.more.favorite': 'smth' }
并返回嵌套对象:
{
person: {
data: {
info: {
more: {
favorite: 'smth',
},
},
},
},
}
最简单的方法是什么?
I want to make function which takes object path, for example
{ 'person.data.info.more.favorite': 'smth' }
and returns nested object:
{
person: {
data: {
info: {
more: {
favorite: 'smth',
},
},
},
},
}
What's the most simple method to do it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您只需要执行单个键/值对,则使用
split
和reduceRight
的nest
简单解决方案-如果您需要将其应用于关键路径可能相互重叠的整个对象,我们可以编写
expand
,它取决于nest
-我们可以编写
expand(o)
从输入对象o
和.map(nest)
中获取Object.entries
代码> 每一个,最后.reduce(merge)
结果 -If you only need to do a single key/value pair, a straightforward solution of
nest
usingsplit
andreduceRight
-If you need to apply this to an entire object where the key paths could possibly overlap with each other, we can write
expand
which depends onnest
-We can write
expand(o)
to takeObject.entries
from the input objecto
and.map(nest)
over each, and finally.reduce(merge)
the result -编辑:这是正确的答案,对不起,我第一次误读了它。
旧答案读了错误的问题:
嘿,您可以使用以下代码来完成此操作。 It will only work with string values though with this implementation
You can see it working here
运行以上代码将登录该代码。
编辑:对不起,我知道您现在在JavaScript中要求使用此答案的答案是JS,但操场仍然是打字稿,但上面是JS。
EDIT: Here is the correct answer sorry I misread it the first time.
OLD ANSWER THAT READ QUESTION WRONG:
Hey you can accomplish this with the below code. It will only work with string values though with this implementation
You can see it working here Typescript playground
Running the above code will log this to the console.
Edit: Sorry I realize now you asked for this in javascript updated the answer to be in JS but the playground is still typescript but the above is JS.
该版本的想法与Mulan的答案相似,但职责的分解不同。它通过我一直在调用
pathentries
的有用的中间格式,这与object.entries.entries
输出的结果平行,但包含完整的路径,而不仅仅是顶级级别特性。对于此示例,它将只是我们的
展开
函数调用object .Entries
在您的输入上,将键分配在点上,然后调用Hydrate
关于结果。水合>水合
在我的常规腰带中,只是一个关于此中间格式的一件有用的事情是,它也可以代表数组,使用整数用于对象属性的数组索引和字符串:
但是要在此处使用该功能,我们可能需要更复杂的字符串格式,例如
{' [0] .foo.bar [0] .qux':42}
,我们需要一个更复杂的解析器,而不是k .split('。'。')
。这并不困难,但会将我们带到更远的地方,就像处理更复杂的系统一样,该系统允许任意琴键,包括引号,括号,括号和时期。This version has a similar idea to the answer from Mulan, but with a different breakdown in responsibilities. It goes through a useful intermediate format that I've been calling
pathEntries
, a parallel to the result ofObject.entries
output, but containing full paths rather than just top-level properties. For this example, it would be justOur
expand
function callsObject .entries
on your input, splits the keys on dots, and then callshydrate
on the result.hydrate
is in my regular utility belt and is just a thin wrapper aroundsetPath
, folding the path entries into a single object. It looks like this:One useful thing about this intermediate format is that it can also represent arrays, using integers for array indices and strings for object properties:
But to use that feature here, we would probably need a more sophisticated string format, something like
{ '[0].foo.bar[0].qux': 42 }
and we'd need a somewhat more sophisticated parser than justk .split ('.')
. This is not difficult, but would take us further afield, as would dealing with the still more sophisticated system that allows for arbitrary string keys, including quotation marks, brackets, and periods.