博客
关于我
关于使用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/

你可能感兴趣的文章
mysql日志文件
查看>>
mysql日志管理学习笔记
查看>>
mysql日志问题定位实用命令
查看>>
MySQL日期时间函数大全
查看>>
mysql时间相减的问题
查看>>
mysql时间表示和计算
查看>>
MySQL是如何做容器测试的?
查看>>
mysql更改数据库表utf-8_修改mysql数据库为 utf-8
查看>>
mysql更改表引擎INNODB为MyISAM的方法总结
查看>>
mysql更新一个表里的字段等于另一个表某字段的值
查看>>
Mysql更新时间列只改日期为指定日期不更改时间
查看>>
MySQL更新锁(for update)摘要
查看>>
mysql更新频率_MySQL优化之如何了解SQL的执行频率
查看>>
mysql替换表的字段里面内容
查看>>
MySQL最多能有多少连接
查看>>
MySQL最大建议行数 2000w,靠谱吗?
查看>>
MySQL有哪些锁
查看>>
MySQL服务器安装(Linux)
查看>>
mysql服务器查询慢原因分析方法
查看>>
mysql服务无法启动的问题
查看>>