javascript - 在 Nodejs 中读取 API 数据并创建对象
问题描述
我的API
回报低于nodejs
.
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
}
我必须创建我必须在其中存储PlanId
和plancode
. 我怎样才能做到这一点。我需要以键值格式存储数据。谁能帮我创建数组或对象?我是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,
});
推荐阅读
- swift - 为什么为 UITableViewDiffableDataSource cellProvider 参数返回 nil tableViewCell?
- c# - 固定行数后,将数据表行拆分为两列
- laravel - 从 laravel 项目创建 docker 映像时作曲家的问题
- functional-programming - 为什么这些 ormap 和 for/or 函数会出错?
- reactjs - 在 Typescript 中递归地将 props 映射到 React 子代
- arrays - 使用 Int 对数组进行排序
- excel - 使用宏关闭并重新打开工作簿后出现“无法运行宏”错误
- c++ - 我的 type_trait 在模板化/非模板化代码中的不同行为
- jquery - 在容器内重新排列 div 并保存新视图的推荐方法
- python - 从二值化图像中提取特征向量