>  기사  >  웹 프론트엔드  >  vue.js에서 배열 중복 제거를 구현하는 방법

vue.js에서 배열 중복 제거를 구현하는 방법

王林
王林원래의
2021-10-08 15:22:534447검색

Vue.js 방법으로 배열 중복 제거 구현: 두 개의 for 루프를 사용하여 각 항목의 ID를 결정합니다(예: [that.positions.map(train=>{that.new_Positions.push( train.trainId)})). .결과...].

vue.js에서 배열 중복 제거를 구현하는 방법

이 기사의 운영 환경: windows10 시스템, vue 2.5.2, thinkpad t480 컴퓨터.

vue.js에서 배열 중복 제거를 달성하려면 for 루프 및 새 세트 사용을 고려할 수 있습니다.

첫 번째 방법:

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.js에서 배열 중복 제거를 구현하는 방법

두 번째 방법:

사용... 새로운 세트 구현

구체적인 코드는 다음과 같습니다:

                            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 training

위 내용은 vue.js에서 배열 중복 제거를 구현하는 방법의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.