找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索本站精品资源

首页 教程频道 查看内容

vue.js如何实现数组去重

作者:模板之家 2021-10-8 19:59 105人关注

vue.js实现数组去重的方法:使用两个for循环来判断每一项的id,如【that.positions.map(train={that.new_Positions.push( train.trainId)})that.resul...】。

vue.js实现数组去重的方法:使用两个for循环来判断每一项的id,如【that.positions.map(train=>{that.new_Positions.push( train.trainId)})that.resul...】。

本文操作环境:windows10系统、vue 2.5.2、thinkpad t480电脑。

vue.js中实现数组去重可以考虑使用for循环和... new set两种方式来实现,一起来看下吧!

第一种方法:

用2个for循环,判断每一项的id

具体代码如下:

                            // that.positions.map(train=>{
                            //     that.new_Positions.push( train.trainId)
                            // })
                            //     that.resultArr = [];//去重后的数组
                            //     var flag;
                            //     for (var i in that.new_Positions){
                            //         flag = true;
                            //         for (var j in that.resultArr) {
                            //             if (that.resultArr[j] == that.new_Positions[i]) {
                            //                 flag = false;
                            //                 break;
                            //             }
                            //         }
                            //         if (flag) {
                            //             that.resultArr.push(that.new_Positions[i]);
                            //         }
                            //     }
                            // console.log("that.resultArr:",that.resultArr)

打印的结果:

28ae84c27a37e510f761ba12de8ff4c.png

第二种方法:

用... new set 实现

具体代码如下:

                            that.positions.map(train=>{
                                that.new_Positions.push(train.trainId)
                            })
 
                            that.new_Positions = [...new Set(that.new_Positions)];
                            console.log("that.resultArr:",that.new_Positions)

学习推荐:php培训

以上就是vue.js如何实现数组去重的详细内容,更多请关注模板之家(www.mb5.com.cn)其它相关文章!


路过

雷人

握手

鲜花

鸡蛋
来自: 网络收集

全部回复(0)