2017-11-06 2 views
0

내가이 나는 독특한 통해 삭제하여 현재 생성 된 목록을 제거하기 위해 어떤 조건에 그러나 원하는 배열 myList를 에 추가됩니다타이프 라이터

this.mylist.push([{key:{"test1":name,"test":desc,"hi i am a test":other,"none":type,"amount":0} }]); 

이 변수가 있다고 가정하자 키 부모를 통해 객체 배열을 제거 나는 값이 나는

시도 중복을

을 방지하기 위해 제거 할 수 있도록 객체 배열을 스플 라이스 할

있도록 키

하지만 난이 아이를 보유하고있는 고유 키를 얻어서 배열을 제거하려고 을 작동하지 않았다 나는 또한 :(

나를 도울 수

 this.mylist.splice(this.mylist.indexOf([{key}]), 1); 

사람을 시도

 CheckBox(values,values,values) { 

     this.bool = (this.mylist.indexOf(key) === -1); 


     if (this.bool) { 
      this.mylist.push([{key:{"key":key,"naam":naam,"beschrijving":beschrijving,"type":type,"aantal":aantal} }]); 
     } 

     else { 
this.mylist.splice(this.mylist.indexOf(key), 1); 

     } 
     } 

위 기능은 사용자가 체크 박스를 클릭 할 때 발생하는 이벤트입니다. true 인 경우 배열은 값으로 채워야합니다. 다른 고유 키 배열이 푸시 문 후 중복을

답변

0

을 방지하기 위해 제거해야합니다, 당신은 배열 인 [{key:{"test1":name,"test":desc,"hi i am a test":other,"none":type,"amount":0} }] 을 밀어 이후 this.mylist 배열의 배열을 포함 당신은과 배열에 액세스 할 수 없습니다 key (위의 배열이 this.mylist.indexOf (key)와 함께 첫 번째 요소로 추가 된 경우 this.mylist [0]에 액세스 할 수 없음)

가능한 해결책은 mylist 유형을 만들 수 있습니다 배열 대신 객체로, 다음과 같이 객체에 요소를 추가 할 수 있습니다.

[{key:{"test1":name,"test":desc,"hi i am a test":other,"none":type,"amount":0} }]; 

this.mylist.key = {"test1":name,"test":desc,"hi i am a 
test":other,"none":type,"amount":0} } 

나중에 체크 박스 기능 뒤에이

CheckBox(values) { 
    this.bool = (this.mylist.key === -1); 
    if (this.bool) { 
     this.mylist.key={"key":key,"naam":naam,"beschrijving":beschrijving,"type":type,"aantal":aantal}; 
    } else { 
     delete this.mylist.key; 
    } 
} 

논리처럼 체크 박스 기능을 사용할 수는 myList에가 키를 포함하는 경우 그 다음으로 myList 할 수있는 키를 추가 존재하지 그리고 경우 제거하면 기능이 확인되는 바와 같이 잘못된 것 같습니다 현재. 이 논리는 최종 목표 인 경우 중복 제거를 올바르게 처리하지 못합니다.