【问题标题】:adding key value to an object in typescript在打字稿中向对象添加键值
【发布时间】:2021-10-06 10:59:35
【问题描述】:

我们如何根据条件将键和值插入到现有的对象数组中,这可以使用 map 解决吗?在一行中? .谢谢

如果来自对象的 dealType 是 === PM Restructure,则插入新的键和值,如果 dealType Partner Location Submission 删除 AnnualRentProposed 和 AnnualRentCurrent。

感谢您的任何想法和帮助。

cashContribution : 3000, storeCashFlow : 4000,

#数据

  this.data.object = [
        {
            "id": 196,
            "name": "Partner Deal ",
            "dealType": "Partner Location Submission",
            "annualRentProposed": null,
            "annualRentCurrent": null,
            "firmTermRemaining": null,
            "firmTermAdded": null,
            "maxAvailableTerm": null,
            "capitalContribution": null,
            "createdOnString": "09/30/2021",
            "fileName": null,
            "serverFileName": null,
            "size": null,
            "absoluteUri": null,
            "sentTo": null
        },
        {
            "id": 197,
            "name": "Buyout Deal Disposition",
            "dealType": "Idle Buyout",
            "annualRentProposed": null,
            "annualRentCurrent": null,
            "firmTermRemaining": null,
            "firmTermAdded": null,
            "maxAvailableTerm": null,
            "capitalContribution": null,
            "createdOnString": "09/30/2021",
            "fileName": null,
            "serverFileName": null,
            "size": null,
            "absoluteUri": null,
            "sentTo": null
        },
        {
            "id": 199,
            "name": "Sublease Deal Disposition",
            "dealType": "Idle Sublease",
            "annualRentProposed": null,
            "annualRentCurrent": null,
            "firmTermRemaining": null,
            "firmTermAdded": null,
            "maxAvailableTerm": null,
            "capitalContribution": null,
            "createdOnString": "09/30/2021",
            "fileName": null,
            "serverFileName": null,
            "size": null,
            "absoluteUri": null,
            "sentTo": null
        },
        {
            "id": 203,
            "name": "Disposition of Location #10532-S",
            "dealType": "PM Restructure",
            "annualRentProposed": null,
            "annualRentCurrent": null,
            "firmTermRemaining": null,
            "firmTermAdded": null,
            "maxAvailableTerm": null,
            "capitalContribution": null,
            "createdOnString": "10/01/2021",
            "fileName": null,
            "serverFileName": null,
            "size": null,
            "absoluteUri": null,
            "sentTo": null
        },
        {
            "id": 214,
            "name": null,
            "dealType": "Approval to Close",
            "annualRentProposed": null,
            "annualRentCurrent": null,
            "firmTermRemaining": null,
            "firmTermAdded": null,
            "maxAvailableTerm": null,
            "capitalContribution": null,
            "createdOnString": "10/04/2021",
            "fileName": null,
            "serverFileName": null,
            "size": null,
            "absoluteUri": null,
            "sentTo": null
        },
        {
            "id": 215,
            "name": "pmpm",
            "dealType": "PM Restructure",
            "annualRentProposed": null,
            "annualRentCurrent": null,
            "firmTermRemaining": null,
            "firmTermAdded": null,
            "maxAvailableTerm": null,
            "capitalContribution": null,
            "createdOnString": "10/05/2021",
            "fileName": null,
            "serverFileName": null,
            "size": null,
            "absoluteUri": null,
            "sentTo": null
        }
    ]

【问题讨论】:

    标签: javascript angular typescript


    【解决方案1】:

    我们可以使用Array.map(),使用一个简单的 .map() 函数。

    我们在 dealType 上使用 switch 语句来决定如何映射每个交易。

    默认情况下,我们不修改交易。

    可以扩展“PM 重组”中使用的逻辑,为其他交易类型添加更多参数。

    const input = [ { "id": 196, "name": "Partner Deal ", "dealType": "Partner Location Submission", "annualRentProposed": null, "annualRentCurrent": null, "firmTermRemaining": null, "firmTermAdded": null, "maxAvailableTerm": null, "capitalContribution": null, "createdOnString": "09/30/2021", "fileName": null, "serverFileName": null, "size": null, "absoluteUri": null, "sentTo": null }, { "id": 197, "name": "Buyout Deal Disposition", "dealType": "Idle Buyout", "annualRentProposed": null, "annualRentCurrent": null, "firmTermRemaining": null, "firmTermAdded": null, "maxAvailableTerm": null, "capitalContribution": null, "createdOnString": "09/30/2021", "fileName": null, "serverFileName": null, "size": null, "absoluteUri": null, "sentTo": null }, { "id": 199, "name": "Sublease Deal Disposition", "dealType": "Idle Sublease", "annualRentProposed": null, "annualRentCurrent": null, "firmTermRemaining": null, "firmTermAdded": null, "maxAvailableTerm": null, "capitalContribution": null, "createdOnString": "09/30/2021", "fileName": null, "serverFileName": null, "size": null, "absoluteUri": null, "sentTo": null }, { "id": 203, "name": "Disposition of Location #10532-S", "dealType": "PM Restructure", "annualRentProposed": null, "annualRentCurrent": null, "firmTermRemaining": null, "firmTermAdded": null, "maxAvailableTerm": null, "capitalContribution": null, "createdOnString": "10/01/2021", "fileName": null, "serverFileName": null, "size": null, "absoluteUri": null, "sentTo": null }, { "id": 214, "name": null, "dealType": "Approval to Close", "annualRentProposed": null, "annualRentCurrent": null, "firmTermRemaining": null, "firmTermAdded": null, "maxAvailableTerm": null, "capitalContribution": null, "createdOnString": "10/04/2021", "fileName": null, "serverFileName": null, "size": null, "absoluteUri": null, "sentTo": null }, { "id": 215, "name": "pmpm", "dealType": "PM Restructure", "annualRentProposed": null, "annualRentCurrent": null, "firmTermRemaining": null, "firmTermAdded": null, "maxAvailableTerm": null, "capitalContribution": null, "createdOnString": "10/05/2021", "fileName": null, "serverFileName": null, "size": null, "absoluteUri": null, "sentTo": null } ]
    
     // Function that maps each deal according to the dealType        
    function mapDeal(deal, parameters) {
        switch (deal.dealType) {
    
            case 'PM Restructure':
                // Add any extra parameters for PM Restructure
                return { ...deal, ...(parameters['PM Restructure'] || {})};
    
            case 'Partner Location Submission':
                // Remove annualRentProposed, annualRentCurrent using object destructuring
                const { annualRentProposed, annualRentCurrent, ...rest } = deal;
                return rest;
             
            default:
                // Leave the deal as it is
                return deal;
        }
    }
     
    function mapDeals(input, parameters) {
        return input.map(deal => mapDeal(deal, parameters));
    }
     
    const parameters = { 
        'PM Restructure': { 
            cashContribution: 3000,
            storeCashFlow: 4000
        }
    }
     
    console.log(mapDeals(input, parameters))
        
    .as-console-wrapper { max-height: 100% !important; top: 0; }

    【讨论】:

    • 先生,还有一个问题,我们如何在打字稿中将数字转换为数量,例如输入是 424109 ,它应该是 424,109 。谢谢。
    • 不是在角度模板中,而是在代码中
    • 您可能想查看 Intl.NumberFormat:developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…。这将根据区域设置规则格式化数字。
    • 我们还需要考虑货币吗?
    • 还有我们用什么轮来将 11.833333 舍入到 11.8
    猜你喜欢
    • 1970-01-01
    • 2016-09-20
    • 2022-01-12
    • 2016-09-10
    • 1970-01-01
    • 2021-11-26
    • 2021-12-17
    • 2020-01-10
    • 1970-01-01
    相关资源
    最近更新 更多