我正在寻找格式化从服务电话收到的数据的格式。我从服务呼叫中收到的数据的格式为:
{
path: "Protocols/abc Protocols",
protocols: [
{
id: {
name: "Dynamic abc",
path1: "Protocols/abc Protocols/abc",
path2: "Protocols/abc Protocols/abc2",
path3: "Protocols/abc Protocols/abc3",
summary: "Provides abc..... "
}
},
{
id: {
name: "Dynamic def",
path1: "Protocols/def Protocols/def ",
path2: "Protocols/def Protocols/def2",
path3: "Protocols/def Protocols/def3",
summary: "Provides def..... "
}
},
{
id: {
name: "Dynamic efg",
path1: "Protocols/def Protocols/efg ",
path2: "Protocols/def Protocols/efg2",
path3: "Protocols/def Protocols/efg3",
}
}
]
}
对于此数据,我的数据模型的格式为:
path: string;
protocols: [
{
id:
{
name: string,
path1: string,
path2: string,
path3: string,
}
}
]
我想删除响应的“ id”部分,使数据模型的格式为:
path: string;
protocols: [
{
name: string,
path1: string,
path2: string,
path3: string,
},
{
name: string,
path1: string,
path2: string,
path3: string,
}
]
我正在尝试创建数组并将每个id {}中的所有内容推送到数组的标准方法。
for (let i = 0; i < result.protocols.length; i++) {
finalResult.push(result.protocols[i].id);
}
是否有更好的方法来实现?我是一名业余Angular6开发人员。需要帮助吗?
答案 0 :(得分:1)
您可以映射到协议数组,并仅返回较低的嵌套对象,而不是包含id
的较高级别的对象
data.protocols = data.protocols.map(i => i.id);
var data={path:"Protocols/abc Protocols",protocols:[{id:{name:"Dynamic abc",path1:"Protocols/abc Protocols/abc",path2:"Protocols/abc Protocols/abc2",path3:"Protocols/abc Protocols/abc3",summary:"Provides abc..... "}},{id:{name:"Dynamic def",path1:"Protocols/def Protocols/def ",path2:"Protocols/def Protocols/def2",path3:"Protocols/def Protocols/def3",summary:"Provides def..... "}},{id:{name:"Dynamic efg",path1:"Protocols/def Protocols/efg ",path2:"Protocols/def Protocols/efg2",path3:"Protocols/def Protocols/efg3"}}]};
data.protocols = data.protocols.map(i => i.id);
console.log(data);