gpt4 book ai didi

javascript - 如何修改服务调用的数据响应

转载 作者:行者123 更新时间:2023-12-03 00:38:48 25 4
gpt4 key购买 nike

我正在寻找格式化从服务调用中收到的数据。我从服务调用收到的数据的格式如下:

{
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 开发人员。需要帮助吗?

最佳答案

您可以映射协议(protocol)数组并简单地返回较低的嵌套对象,而不是包含 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);

关于javascript - 如何修改服务调用的数据响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53548633/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com