Vue.js method to implement array deduplication: use two for loops to determine the id of each item, such as [that.positions.map(train=>{that.new_Positions.push( train. trainId)})that.resul...].
The operating environment of this article: windows10 system, vue 2.5.2, thinkpad t480 computer.
To achieve array deduplication in vue.js, you can consider using for loop and...new set. Let’s take a look!
The first method:
Use 2 for loops to determine the id of each item
The specific code is as follows:
// 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)
The printed result:
Second method:
Use... new set to achieve
The specific code is as follows:
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)
Learning recommendation: php training
The above is the detailed content of How to implement array deduplication in vue.js. For more information, please follow other related articles on the PHP Chinese website!