vue.js實現(xiàn)數(shù)組去重的方法:使用兩個for循環(huán)來判斷每一項的id,如【that.positions.map(train=>{that.new_Positions.push( train.trainId)})that.resul…】。
本文操作環(huán)境:windows10系統(tǒng)、vue 2.5.2、thinkpad t480電腦。
vue.js中實現(xiàn)數(shù)組去重可以考慮使用for循環(huán)和… new set兩種方式來實現(xiàn),一起來看下吧!
第一種方法:
用2個for循環(huán),判斷每一項的id
具體代碼如下:
// that.positions.map(train=>{ // that.new_Positions.push( train.trainId) // }) // that.resultArr = [];//去重后的數(shù)組 // 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)
打印的結(jié)果:
第二種方法:
用… new set 實現(xiàn)
具體代碼如下:
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培訓(xùn)