天天看點

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