博客
关于我
关于使用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慢查询-开启慢查询
查看>>
MySQL慢查询问题排查
查看>>
mysql截取sql语句
查看>>
mysql手工注入
查看>>
Mysql执行update by id的过程
查看>>
mysql执行计划
查看>>
MySQL执行计划 EXPLAIN参数
查看>>
MySQL执行计划【explain】,看这一篇就够啦!
查看>>
Mysql执行计划字段解释
查看>>
MySQL执行计划解读
查看>>
mysql执行顺序与索引算法
查看>>
mysql技能梳理
查看>>
Mysql插入数据从指定选项中随机选择、插入时间从指定范围随机生成、Navicat使用存储过程模拟插入测试数据
查看>>
mysql操作数据表的命令_MySQL数据表操作命令
查看>>
mysql支持表情
查看>>