안녕하세요 Angular2 및 Typescript 사용 중에 배열에서 특정 인덱스를 삭제하려고합니다. 값에서 인덱스를 검색하고 싶습니다. 내 배열이 정상 선언Angular2 IndexOf 배열 값 찾기 및 삭제
...
RightList = [
'Fourth property',
'Fifth property',
'Sixth property',
]
나는 내 제거 기능을 설정하는 기본 전제로 시작합니다.
removeSel(llist: ListComponent, rlist:ListComponent){
this.selectedllist = llist;
console.log(JSON.stringify(this.selectedllist)); // what is to be searched turned into a string so that it may actually be used
내 JSON.Stringify의 console.log에서 제거하려고 시도하는 값이 "여섯 번째 속성"임을 알립니다. 그러나 다음 코드를 사용하여 배열에서이 값을 찾으려고 시도합니다. 배열에 내 값이 없다는 것을 -1을 반환합니다. 이 검색 될 수 있지만 배열
CONSOLE OUTPUT:
"Sixth property" // Item to be searched for
-1 // not found
에서 항목을 찾을 수없는 항목을 반환하지 콘솔에 내 출력에
var rightDel = this.RightList.indexOf((JSON.stringify(this.selectedllist))); // -1 is not found 1 = found
console.log(rightDel);
내 기능의 내 구현 뭔가 문제가 있습니까 배열을 검색합니까? JSON 캐릭터 라인 문자열 리터럴 따옴표를 인코딩하기 때문입니다
JSON.stringify(this.selectedllist) !== this.selectedllist
때문에