在 Nodejs 中读取 API 的数据并创建对象



我的APInodejs返回以下数据。

console.log(body)打印以下内容。

{
"items": [
{
"PlanId": 2007,
"PlanCode": "Future Mat Cost Planning  - Budget",
"Description": "Future Mat Cost Planning  - Budget",
"FromMonth": "2019-10-01T00:00:00+00:00"
},
{
"PlanId": 3001,
"PlanCode": "Nvidia Cost PL",
"Description": "Nvidia Cost PL",
"FromMonth": "2019-10-01T00:00:00+00:00"
},
{
"PlanId": 1001,
"PlanCode": "Material Cost Planning - PO",
"Description": "Material Cost Planning - PO",
"FromMonth": "2019-10-01T00:00:00+00:00"
}
],
"count": 5,
"hasMore": true,
"limit": 5,
"offset": 0
}

我必须创建对象,在其中存储PlanIdplancode。我该怎么做。我需要以键值格式存储数据。任何人都可以帮我创建数组或对象吗?我是 nodejs 的新手 我想要这样的结果。

obj=[{
"PlanId": 3001,
"PlanCode": "Nvidia Cost PL",
},
{
"PlanId": 1001,
"PlanCode": "Material Cost Planning - PO",
}
];

所以你想map对新对象的响应吗?

// assuming `body` is the API response
const obj = body
.items
.map(item => ({
PlanId: item.PlanId,
PlanCode: item.PlanCode,
});

最新更新