首页 > 解决方案 > 用该数组的第一个元素替换对象中的每个数组

问题描述

我有一个这样的json:

   "Client" : {
        "ClientId" : "eertertwetw",
        "Username" : "c.client",
        "Names" : [
            {
                "Family" : "ClientFamilyName",
                "Given" : [
                    "ClientGivenName"
                ]
            }
        ]
    }

这个json不是固定的,所以有时候有一些属性,有时候没有。我需要用该数组的第一个元素替换此 Json 中的每个数组。因此,例如,在这种情况下,它就像

   "Client" : {
        "ClientId" : "eertertwetw",
        "Username" : "c.client",
        "Names" :
            {
                "Family" : "ClientFamilyName",
                "Given" : 
                    "ClientGivenName"
            }
        ]
    }

任何人都可以帮助我找到一种使用 Typescript 的方法吗?

标签: jsonangulartypescript

解决方案


let data = { Client: {...} }; // your data

data = data.map(client => {
    if (!Object.hasOwnProperty(client.Names, 'Prefix')) {
        client.Names.Prefix = null;
    }
    return client;
});

推荐阅读