2017-09-15 55 views
0
export default [ 
{ 
    user:{ 
     id: '1', 
     person: 'Theodore Roosevelt', 
     text: 'Believe you can and you\'re halfway there', 
     icon:'man' 
    } 
}, 
{ 
    user:{ 
     id: '2', 
     person: 'Normale', 
     text: 'Change your thoughts and you change your world.', 
     icon:'woman' 
    } 

}, { 
    user:{ 
     id: '3', 
     person: 'Thlt', 
     text: 'Believe you can and you\'re halfway there', 
     icon:'man' 
    } 
}] 

上面的代碼是離子工具中數據文件夾下的ts文件。
我想在id的基礎上通過點擊每個條目前面的刪除按鈕從這個數組中刪除一個條目。 我是新來的離子。我試圖.slice沒有工作如何刪除角2和離子json中的對象

+0

表明您已經嘗試了什麼 – Sajeetharan

回答

0

const arr = [{ 
 
    user:{ 
 
     id: '1', 
 
     person: 'Theodore Roosevelt', 
 
     text: 'Believe you can and you\'re halfway there', 
 
     icon:'man' 
 
    } 
 
}, 
 
{ 
 
    user:{ 
 
     id: '2', 
 
     person: 'Normale', 
 
     text: 'Change your thoughts and you change your world.', 
 
     icon:'woman' 
 
    } 
 

 
}, { 
 
    user:{ 
 
     id: '3', 
 
     person: 'Thlt', 
 
     text: 'Believe you can and you\'re halfway there', 
 
     icon:'man' 
 
    } 
 
}] 
 

 
const id = '2'; // 2 will be removed 
 
const result = arr.filter(item => (item.user || {}).id !== id) 
 
console.log(result)