博客
关于我
关于使用map,for等遍历数组获取其中每一项的值在调用接口只取到最后一个值的问题
阅读量:695 次
发布时间:2019-03-15

本文共 1139 字,大约阅读时间需要 3 分钟。

接上一篇文章;

这里循环一个数组list,拿到其中的某些值,去做参数执行下一步的方法,需要这个参数(params)是变化的,但是在sendRightsParams方法中,输出的值总是数组的最后一项里面的内容,

getRights(list) {               const paramsKey = this.mediaForRights[this.type].keys.value;            if (list && list.length) {                   list.map((item, index) => {                       const params = item.name;                    const temp = item[this.mediaForRights[this.type].keys.id];                    this.$set(params, paramsKey, temp);                    this.sendRightsParams(params, index);                });            }        },

造成的原因:

这里给paramsKey赋值的话,每次重新的遍历会覆盖掉之前的值;
修改之后的代码

getRights(list) {               const paramsKey = this.mediaForRights[this.type].keys.value;            if (list && list.length) {                   list.map((item, index) => {                       const params = Object.assign({   }, item.name);                    const temp = item[this.mediaForRights[this.type].keys.id];                    this.$set(params, paramsKey, temp);                    this.sendRightsParams(params, index);                });            }        },

另外:如果采用var来定义变量的话可能会经常遇到变量的作用域、变量提升问题;一般使用let比较好。

转载地址:http://qgfmz.baihongyu.com/

你可能感兴趣的文章
navicat连接远程mysql数据库
查看>>
Navicat通过存储过程批量插入mysql数据
查看>>
Navicat(数据库可视化操作软件)安装、配置、测试
查看>>
navigationController
查看>>
NB-IOT使用LWM2M移动onenet基础通信套件对接之APN设置
查看>>
NBear简介与使用图解
查看>>
Vue过滤器_使用过滤器进行数据格式化操作---vue工作笔记0015
查看>>
Ncast盈可视 高清智能录播系统 IPSetup.php信息泄露+RCE漏洞复现(CVE-2024-0305)
查看>>
NCNN中的模型量化解决方案:源码阅读和原理解析
查看>>
NCNN源码学习(1):Mat详解
查看>>
nc命令详解
查看>>
NC综合漏洞利用工具
查看>>
ndarray 比 recarray 访问快吗?
查看>>
ndk-cmake
查看>>
NdkBootPicker 使用与安装指南
查看>>
ndk特定版本下载
查看>>
NDK编译错误expected specifier-qualifier-list before...
查看>>
Neat Stuff to Do in List Controls Using Custom Draw
查看>>
Necurs僵尸网络攻击美国金融机构 利用Trickbot银行木马窃取账户信息和欺诈
查看>>
Needle in a haystack: efficient storage of billions of photos 【转】
查看>>