将嵌套对象中的对象向上提起
我有一个树对象。在其最深层面上,有_Metrics
带有值的密钥,我想要的是将_Metrics
提升到其父对象。 我正在努力解决此案,因为这是我第一次遇到这样的情况。
给定输入
const d = {
"enterprise1": {
"site1": {
"area1": {
"unit1": {
"__metrics": {
"state_class": "down",
"state": "other",
"reason": "UNDEFINED",
"product": "sovereign",
"count": 10
}
},
"unit2": {
"__metrics": {
"state_class": "run",
"state": "running",
"reason": "UNDEFINED",
"product": "cola",
"count": 10
}
}
}
}
}
};
上面的输入将始终在第四级中具有量表
对象,即“ unit”
对象。
所需的输出
const d = {
"enterprise1": {
"__metrics": {
"state_class": ["run", "down"],
"state": ["running", "other"],
"reason": "UNDEFINED",
"product": ["cola", "sovereign"],
"count": 20
},
"site1": {
"__metrics": {
"state_class": ["run", "down"],
"state": ["running", "other"],
"reason": "UNDEFINED",
"product": ["cola", "sovereign"],
"count": 20
},
"area1": {
"__metrics": {
"state_class": ["run", "down"],
"state": ["running", "other"],
"reason": "UNDEFINED",
"product": ["cola", "sovereign"],
"count": 20
},
"unit1": {
"__metrics": {
"state_class": "down",
"state": "other",
"reason": "UNDEFINED",
"product": "sovereign",
"count": 10
}
},
"unit2": {
"__metrics": {
"state_class": "run",
"state": "running",
"reason": "UNDEFINED",
"product": "cola",
"count": 10
}
}
}
}
}
};
I have a tree object. In its deepest level there is _metrics
key with value and what I want is to lift _metrics
up to its parent object.
I am struggling to solve this case as it is the first time I encounter such a case.
Given Input
const d = {
"enterprise1": {
"site1": {
"area1": {
"unit1": {
"__metrics": {
"state_class": "down",
"state": "other",
"reason": "UNDEFINED",
"product": "sovereign",
"count": 10
}
},
"unit2": {
"__metrics": {
"state_class": "run",
"state": "running",
"reason": "UNDEFINED",
"product": "cola",
"count": 10
}
}
}
}
}
};
The input above will always have __metrics
object in 4th level which is "unit"
object.
Desired output
const d = {
"enterprise1": {
"__metrics": {
"state_class": ["run", "down"],
"state": ["running", "other"],
"reason": "UNDEFINED",
"product": ["cola", "sovereign"],
"count": 20
},
"site1": {
"__metrics": {
"state_class": ["run", "down"],
"state": ["running", "other"],
"reason": "UNDEFINED",
"product": ["cola", "sovereign"],
"count": 20
},
"area1": {
"__metrics": {
"state_class": ["run", "down"],
"state": ["running", "other"],
"reason": "UNDEFINED",
"product": ["cola", "sovereign"],
"count": 20
},
"unit1": {
"__metrics": {
"state_class": "down",
"state": "other",
"reason": "UNDEFINED",
"product": "sovereign",
"count": 10
}
},
"unit2": {
"__metrics": {
"state_class": "run",
"state": "running",
"reason": "UNDEFINED",
"product": "cola",
"count": 10
}
}
}
}
}
};
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我猜 javascript 没有一个库来提升对象的键值。您可以使用 for-in 循环在每个级别中添加 __metrics 。 __metrics 的添加取决于对象的深度。
循环将是这样的:
代码的整体可能是这样的:
也许这不是最佳实践,但这几乎是您所期望的。如果在将数据对象添加到表中时在每个级别都添加 __metrics 会更好。希望你喜欢它:)
I guess javascript doesn't have a library to lift it up the key-value of an object. You could be use to add
__metrics
in each level using for-in loop. The add of__metrics
is depend how many level deep the object do you have.The looping would be like this:
And the overall of the code could be like this:
maybe it's not the best practice, but it's almost what you'd expect. it would be better if
__metrics
were added at each level when the data objects were added to the table. Hope you like it :)