用Javascript缩小或展平一个对象?

时间:2018-10-03 22:37:01

标签: javascript lodash

我有一个正在使用的数据对象是动态的,并且来自数据库调用。如果记录包含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的深度,并且可以将数据移动到父级,那将是理想的

我应该采用哪种方法处理对象类型?

2 个答案:

答案 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更具可读性(通过mergeWithomit),如下所示:

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>