2017-05-18 24 views
0

我想刪除一個對象而不獲取對象的索引。在對象我有以下數據,即如何刪除對象而不獲取角度爲2的索引

 
{ 
    "id" : 1, 
    "address": "abc ghsddsj", 
    "phone" : "123467891" 
}, 
{ 
    "id" : 2, 
    "address": "abdffddfgc ghsddsj", 
    "phone" : "7864545645" 
} 

//這裏是我的代碼刪除對象

import { Injectable } from "@angular/core"; 
import { Router } from '@angular/router'; 
@Injectable() 
export class AppService{ 
    addressList : any[] = []; 
    newAddress: any; 
    constructor (public _router:Router){ } 
    updateData(){ 
     this.addressList.push(this.newAddress); 
    } 
    getData(){ 
     return this.addressList; 
    } 
    deleteAddress(id:any){ 
     //let index = this.addressList.indexOf(address); 
     //this.addressList.splice(index,1); 
     for(let address of this.addressList){ 
      if(address.id == id){ 
       // expression code will be here 
      } 
     } 
    } 
} 

回答

0

您可以使用過濾器。這樣的事情:

this.addresList = this.addresList.filter(address => (address.id !== id)); 
+0

嗨巴特感謝您給出的答案,但它不工作。 – shubham

+0

也許問題是你也有id「1」兩次。哦,你應該用這行代替for循環。 :) – Bart

相關問題