如何从角度2和离子的json中删除对象

时间:2017-09-15 08:31:02

标签: json angular ionic2

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它没有工作

1 个答案:

答案 0 :(得分: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)