使用映射将嵌套的JS对象转换为另一个
我想使用映射将AST结构中的嵌套JS对象转换为另一个,它可以是任何级别的深处。任何帮助将得到深深的赞赏。如果这很方便,我们也可以使用lodash。
动机: - 供应数据可以采用多种格式,我需要使用不同的供应对象创建标准产品,我们可以为其创建标准产品模型和几个用于供应产品模型的映射。
如果您认为可以通过更好的产品模型或地图模型来解决此问题,那么我将非常感谢这些建议。供应模型仍然是固定的。
数据
const source = {
a:1,
b:2,
c:7,
children:[
{
a:3,
b:7,
d:8,
children: [
{
a:6,
b:8,
x:4
}
]
},
{
a:5,
b:3,
c:9
}
]
}
映射
const map = {
c:'a',
d:'b',
elements: {
identifier:'children', // which field to lookout for children
ofType: 'map' // every child has similar structure as parent
}
}
这是我期望的输出
const output = {
c:1,
d:2,
elements:[
{
c:3,
d:7,
elements:[
{
c:6,
d:8
}
]
},
{
c:5,
d:3
}
]
}
I want to transform a nested JS object in AST structure into another using mappings, It can be any level deep. Any help is deeply appreciated. We can use lodash as well if that is convenient.
Motivation :- Supply data can be in several formats, I need to create standard products using different supply objects, for which we can create standard product model and several mappings for product model to supply.
If you think this problem can be solved with a better data modeling of product model or map model, I 'll highly appreciate the suggestions. The supply model is fixed nonetheless.
Data
const source = {
a:1,
b:2,
c:7,
children:[
{
a:3,
b:7,
d:8,
children: [
{
a:6,
b:8,
x:4
}
]
},
{
a:5,
b:3,
c:9
}
]
}
Mappings
const map = {
c:'a',
d:'b',
elements: {
identifier:'children', // which field to lookout for children
ofType: 'map' // every child has similar structure as parent
}
}
Here is the output i am expecting
const output = {
c:1,
d:2,
elements:[
{
c:3,
d:7,
elements:[
{
c:6,
d:8
}
]
},
{
c:5,
d:3
}
]
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是可以使用我在
Here is how it could be solved using tool I'm working on ????Putout:
Check out in ????Putout Editor.
What it does is searches for
ObjectProperties
and then does the of renaming.Notes:
options
support from the boxfrom
andto
indefaultMap
it simplifies things a lot ;)