簡體   English   中英

在Nodejs中讀取API的數據並創建object

[英]Reading data of API in Nodejs and creating object

我的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
}

我必須創建 object ,我必須在其中存儲PlanIdplancode 我怎樣才能做到這一點。 我需要以鍵值格式存儲數據。 誰能幫我創建數組或 object? 我是nodejs的新手,我想要這樣的結果。

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

所以你想map響應一個新的 object?

// assuming `body` is the API response

const obj = body
  .items
  .map(item => ({
    PlanId: item.PlanId,
    PlanCode: item.PlanCode,
  });

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM