天天看点

vue数组去重2种方法

方法一:用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)

打印的结果:

vue数组去重2种方法

方法二:用... 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)

 打印的结果:

vue数组去重2种方法
vue