我有一个正在使用的数据对象是动态的,并且来自数据库调用。如果记录包含approval
详细信息,它将包含在我返回的对象中,否则将被省略。
我的问题是我有一种导出方法,该方法可以将对象转换为excel文件,但这仅使用对象的第一级,因此缺少可能的批准数据。
我试图弄清楚如何展平或缩小对象,以便将所有键/值都带到父级。
示例代码:
// Current Data
var obj = [{
"toolSuite": "Enterprise Product",
"toolStatus": "Active",
"toolOwnerGroup": "PD",
"toolTier": "1",
"infoSec": "Yes",
"sso": "?",
"toolPortfolio": "Enterprise Product",
"NeedByDate": "29-Jun-2018",
"approvals": {
"approval": { <--- Need to move all this data to the parent level
"ApprovalID": "139",
"TaskID": "232",
"SubmissionDate": "2018-10-03T22:19:24.153",
"WhoSubmitted": "Q1234",
"Approver": "Q5678",
"IsCanceled": "0",
"ApproverFirst": "Bob",
"ApproverLast": "Builder",
"ApproverNTID": "bbuilder"
}
}
}, {
"toolSuite": "Enterprise Product",
"toolStatus": "Active",
"toolOwnerGroup": "PD",
"toolTier": "1",
"infoSec": "Yes",
"sso": "?",
"toolPortfolio": "Enterprise Product",
"NeedByDate": "29-Jun-2018",
"ApprovalID": "139"
}]
// Desired Output
var obj = [{
"toolSuite": "Enterprise Product",
"toolStatus": "Active",
"toolOwnerGroup": "PD",
"toolTier": "1",
"infoSec": "Yes",
"sso": "?",
"toolPortfolio": "Enterprise Product",
"NeedByDate": "29-Jun-2018",
"ApprovalID": "139",
"TaskID": "232",
"SubmissionDate": "2018-10-03T22:19:24.153",
"WhoSubmitted": "Q1234",
"Approver": "Q5678",
"IsCanceled": "0",
"ApproverFirst": "Bob",
"ApproverLast": "Builder",
"ApproverNTID": "bbuilder"
}, {
"toolSuite": "Enterprise Product",
"toolStatus": "Active",
"toolOwnerGroup": "PD",
"toolTier": "1",
"infoSec": "Yes",
"sso": "?",
"toolPortfolio": "Enterprise Product",
"NeedByDate": "29-Jun-2018",
}]
在上面的代码中,第一个对象具有approvals.approval
数据。我正在尝试将此数据带到父级。它不是数据数组,因此不会有重复的键。
我目前在我的项目中将lodash
用于其他项目,但找不到任何可以轻松执行此操作的东西。
在这种情况下,我需要reduce
还是flatten
吗?它应该永远是approvals.approval
,但是如果有一种方法我可以指定3
的深度,并且可以将数据移动到父级,那将是理想的
我应该采用哪种方法处理对象类型?
答案 0 :(得分:3)
不需要库,只需将Ruby -v 2.4.2p198
对象传播或Object.assign
到父对象中,然后删除approval
键:
approvals
如果var obj=[{"toolSuite":"Enterprise Product","toolStatus":"Active","toolOwnerGroup":"PD","toolTier":"1","infoSec":"Yes","sso":"?","toolPortfolio":"Enterprise Product","NeedByDate":"29-Jun-2018","approvals":{"approval":{"ApprovalID":"139","TaskID":"232","SubmissionDate":"2018-10-03T22:19:24.153","WhoSubmitted":"Q1234","Approver":"Q5678","IsCanceled":"0","ApproverFirst":"Bob","ApproverLast":"Builder","ApproverNTID":"bbuilder"}}},{"toolSuite":"Enterprise Product","toolStatus":"Active","toolOwnerGroup":"PD","toolTier":"1","infoSec":"Yes","sso":"?","toolPortfolio":"Enterprise Product","NeedByDate":"29-Jun-2018","ApprovalID":"139"}];
Object.assign(obj[0], obj[0].approvals.approval);
delete obj[0].approvals;
console.log(obj[0]);
数组中的多个项目具有需要传输的obj
属性,则使用循环:
approvals: { approval: { ... } }
如果可能的话,我还建议将您的obj.forEach((object) => {
const { approvals } = object;
if (!approvals) return;
Object.assign(object, approvals.approval);
delete object.approvals;
});
数组重命名为其他数组-这是一个数组,而不是普通对象,因此可以将其命名为obj
以避免混淆。
答案 1 :(得分:0)
使用lodash
更具可读性(通过mergeWith和omit),如下所示:
var data = { "toolSuite": "Enterprise Product", "toolStatus": "Active", "toolOwnerGroup": "PD", "toolTier": "1", "infoSec": "Yes", "sso": "?", "toolPortfolio": "Enterprise Product", "NeedByDate": "29-Jun-2018", "approvals": { "approval": { "ApprovalID": "139", "TaskID": "232", "SubmissionDate": "2018-10-03T22:19:24.153", "WhoSubmitted": "Q1234", "Approver": "Q5678", "IsCanceled": "0", "ApproverFirst": "Bob", "ApproverLast": "Builder", "ApproverNTID": "bbuilder" } } }
const result = _(data)
.mergeWith(data.approvals.approval)
.omit('approvals')
.value()
console.log(result)
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.min.js"></script>
在将data
作为数组的情况下,您可以简单地map进行操作,因为您确实希望得到与输入相同数量的输出:
var data = [{ "toolSuite": "Enterprise Product", "toolStatus": "Active", "toolOwnerGroup": "PD", "toolTier": "1", "infoSec": "Yes", "sso": "?", "toolPortfolio": "Enterprise Product", "NeedByDate": "29-Jun-2018", "approvals": { "approval": { "ApprovalID": "139", "TaskID": "232", "SubmissionDate": "2018-10-03T22:19:24.153", "WhoSubmitted": "Q1234", "Approver": "Q5678", "IsCanceled": "0", "ApproverFirst": "Bob", "ApproverLast": "Builder", "ApproverNTID": "bbuilder" } } }, { "toolSuite": "Enterprise Product", "toolStatus": "Active", "toolOwnerGroup": "PD", "toolTier": "1", "infoSec": "Yes", "sso": "?", "toolPortfolio": "Enterprise Product", "NeedByDate": "29-Jun-2018" }]
const result = _.map(data, obj => _(obj)
.mergeWith(_.get(obj,'approvals.approval'))
.omit('approvals')
.value())
console.log(result)
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.min.js"></script>