javascript - 用JS中现有数组的一些值创建一个新数组
问题描述
我拥有的数组如下。我们称之为数组reduction
。
var reduction = [
{
ReductionID: 5813,
PatiendID: 945994,
ProviderAcctNumber: "",
Discharge: "945994 : 01/01/0001 - 01/01/001",
Selected: 1,
Balance: 20,
Charges: 10
}
];
我想要一个新数组 ( patientDetails
),格式中只有两个值
{
PID: 945994 // which is PatientID in the reduction array
selection: 1// which is Selected in the reduction array
}
我在第二个数组中想要的是:
- 第一个数组中只有两个值(减少)
- 这两个名称在第二个数组中应该不同(即,PatientID 应该是 PID)
- 值与第一个数组中的值相同
有可能与map()
功能有关吗?
解决方案
你可以这样做
var arr = [{
ReductionID:5813,
PatientID: 945994,
ProviderAcctNumber:"",
Discharge: "945994 : 01/01/0001 - 01/01/001",
Selected: 1,
Balance: 20,
Charges: 10
}];
var updatedArr = arr.map((obj)=>{
return {
PID:obj.PatientID,
selection:obj.Selected
}
});
console.log(updatedArr);
map
在这里,我们使用并选择我们需要的值并根据需要返回对象来遍历原始数组
推荐阅读
- java - 我可以使用正则表达式来匹配 java 中的预定义字符串吗?
- amazon-dynamodb - DynamoDB 仅索引一组特定的值
- ios - UITableViewCell 显示不正确的字符串
- perl - 当 LWP::UserAgent 连接到代理时,是否可以添加一些调试输出?
- python - 读取格式化 LaTeX 块中导出的值数组?
- visual-studio-code - WSL环境中的VSCode任务,尝试运行shell脚本时终端一直退出
- elixir - 如何 unix 将一个命令的输出通过管道传输到 Elixir 的混合任务中?
- python - 美丽的汤 find_all() 方法抓取的标签比过滤器指定的要多
- android - 如果主活动中有 Fragment,则在哪里更新 UI 元素
- javascript - 使用 Google Apps 脚本填写保存在我的 Google Drive 上的 PDF 表单