javascript - 使用 es6 map 更改数组中元素的特定属性
问题描述
我的数据中有数组,我需要遍历数组,对于 id 与我需要编辑的数组匹配的任何数组,我只需要更改属性标题,它是一个带有另一个名称的字符串,例如, JOhn' 我正在迭代的数组的格式为
[{trackStatus: "ic", dates: Array(0), _id: "5b8510d4458a656d3a6cbf14", title: "martin"} ,{trackStatus: "ic", dates: Array(0), _id: "5b8510d4458a656d3a6cbf16", title: "rama" }]
现在我遵循了这个策略
array1: array1.map((topic) => {
if( topic._id === action.payload.data._id){
console.log('here');
return{
...topic,
title:'yama'
}
}
return topic;
});
上述策略是否正确,因为我需要了解我面临的问题或错误是因为这部分还是其他原因?
解决方案
如果匹配,您可以简单地更改title
属性:id
let array1 = [{trackStatus: "ic", dates: Array(0), _id: "5b8510d4458a656d3a6cbf14", title: "martin"} ,{trackStatus: "ic", dates: Array(0), _id: "5b8510d4458a656d3a6cbf16", title: "rama" }];
let action = {};
action.payload = {};
action.payload.data={};
action.payload.data._id = '5b8510d4458a656d3a6cbf14';
array1 = array1.map((topic) => {
if( topic._id === action.payload.data._id){
topic.title = 'yama';
}
return topic;
});
console.log(array1);
推荐阅读
- c# - 在 Visual Studio 2019 中替换 C# 的数据源
- google-cloud-platform - 我可以在 Cloud Data Fusion 中创建多少个管道?
- c# - 为什么我不能将命令绑定到列表框命令的双击
- taskwarrior - 如何关闭taskwarrior中的多个任务?
- c# - 如何在 LINQ c# 中使用枚举进行分组并找到最大值
- rust - 如何从以前的宏生成代码生成宏?
- python - Python:错误 GoogleAdsServerFault:[UserListError.USER_LIST_MUTATE_NOT_SUPPORTED
- python - 使用浮点数和前导零格式化 pandas 数据帧
- python - 我认为,Mac python/pipenv 环境被 Saltstack 安装搞砸了?
- sparql - 距离,使用 Sparql 的多边形