vue.js如何实现数组去重

vue.js如何实现数组去重,第1张

vue.js如何实现数组去重

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)

打印的结果:

第二种方法:

用... 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如何实现数组去重的详细内容,

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/697078.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-04-22
下一篇 2022-04-22

发表评论

登录后才能评论

评论列表(0条)

保存