Home > Web Front-end > Vue.js > body text

How to implement array deduplication in vue.js

王林
Release: 2021-10-08 15:22:53
Original
4491 people have browsed it

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...].

How to implement array deduplication in vue.js

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)
Copy after login

The printed result:

How to implement array deduplication in vue.js

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)
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template