如何从数组中删除对象

时间:2017-09-01 09:59:18

标签: angular typescript

我在html页面上列出了数组对象,当我点击其中的任何一个时,我得到了它的信息,让我解释一下我是如何做到这一点的

我的test.ts文件

this.fetchdata = JSON.parse(localStorage.getItem('education'));
 log(elem) { 
    console.log(elem); 

  }

我的test.html

 <ul id="elements">
          <li *ngFor="let elem of fetchdata" (click)="log(elem)">
              {{elem.title}} {{elem.description}}
          </li>
      </ul>

当我点击我在控制台中看到这个 - enter image description here

如何从密钥education中删除本地存储数组中的点击记录?

2 个答案:

答案 0 :(得分:3)

你的日志功能应该是,

log(elem :any){
   let objDelete = this.fetchdata.indexOf(elem , 0);
   if (objDelete > -1) {
    this.fetchdata.splice(objDelete, 1);
  }
}

答案 1 :(得分:3)

跟踪索引

*ngFor="let elem of fetchdata; let index = index"

deleteItem(index){
    this.fetchdata.splice(index, 1);
}
相关问题