Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
355 views
in Technique[技术] by (71.8m points)

javascript - 无法删除[对象数组]的属性“ 1”(Cannot delete property '1' of [object Array])

get list with Input in my components :(在我的组件中获取带有Input列表:)

@Input() usersInput: Section[]; export interface Section { displayName: string; userId: number; title: number; } and this is the value list :(这是值列表:) 0: displayName: "???? ???" firstName: null lastName: null title: 0 userId: 1 1: displayName: "???????? ????????" firstName: "????????" lastName: "????????" title: 0 userId: 2 in ngAfterViewInit i set the input Value to users List :(在ngAfterViewInit我将输入值设置为用户列表:) ngAfterViewInit(): void { this.users = this.usersInput; if (this.users.length === 0) { this.show = false; } else { this.show = true; } } this is Users :(这是用户:) users: Section[] = []; and i use it in html List :(我在html List中使用它:) <div *ngFor="let item of users" class="d-flex selected-list-items mt-3"> <div class="col-md-5 col-lg-5 col-xs-5 col-sm-5 col-xl-5"> <label>{{item.displayName}}</label> </div> <div class="col-md-5 col-lg-5 col-xs-5 col-sm-5 col-xl-5"> <label> {{ getEnumTranslate(item.title)}}</label> </div> <div class="justify-content-center col-md-2 col-lg-2 col-xs-2 col-sm-2 col-xl-2"> <button (click)="deleteUser(item.userId)" mat-button> <mat-icon aria-label="Delete" color="accent">delete</mat-icon> </button> </div> </div> now when i need to use delete button :(现在当我需要使用删除按钮时:) <button (click)="deleteUser(item.userId)" mat-button> <mat-icon aria-label="Delete" color="accent">delete</mat-icon> </button> ts :(ts:) deleteUser(id: number): void { let userModel = {} as Section; userModel = this.users.find(x => x.userId === id); const index = this.users.indexOf(userModel); this.users.splice(index, 1); this.emitValueModel.push( { title: this.user.title, userId: this.user.userId } ); this.selectedUserId.emit(this.emitValueModel); if (this.users.length === 0) { this.show = false; } this.cdref.detectChanges(); } it show me this error :(它告诉我这个错误:) ERROR TypeError: Cannot delete property '1' of [object Array](错误TypeError:无法删除[对象数组]的属性'1') whats the problem???(有什么问题???) how can i solve that ?(我该如何解决?)   ask by kianoush dortaj translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Try:(尝试:)

deleteUser(id) { const index = this.users.findIndex(x => x.userId === id); this.users.splice(index, 1); } Working Demo(工作演示)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...