如何根据某些条件从对象数组中删除元素

时间:2018-03-12 07:19:54

标签: javascript angular typescript

我正面临角度

的严重问题

我有以下数组

dataArray (3) [21, 21, 23]

以下是API的实际数据

ViewData = [  
      {  
         "id":5,
         "name":"Private",
         "description":"",

      },
      {  
         "id":7,
         "name":"Semi Private",
         "description":"",

      },
      {  
         "id":8,
         "name":"laboratory",
         "description":"",

      },
      {  
         "id":15,
         "name":"Test",

      },
      {  
         "id":16,
         "name":"Testss",
         "description":null,

      },
      {  
         "id":18,
         "name":"TestSan",
         "description":null,

      },
      {  
         "id":21,
         "name":"TestBBB",
         "description":"test",

      },
      {  
         "id":23,
         "name":"TestOne",
         "description":null,

      },
      {  
         "id":2,
         "name":"Standard ward",
         "description":"",
         "sharing":4,
         "set_value":2
      }
   ]

我必须在处理之后处理ViewData(第二个数组)ViewData不应该包含DataArray中存在id的JSON对象(我的意思是如果ViewData的任何id等于DataArray中的id我要删除那个JSON对象来自查看数据)    (输出应该如下)

ViewData = [  
      {  
         "id":5,
         "name":"Private",
         "description":"",

      },
      {  
         "id":7,
         "name":"Semi Private",
         "description":"",

      },
      {  
         "id":8,
         "name":"laboratory",
         "description":"",

      },
      {  
         "id":15,
         "name":"Test",

      },
      {  
         "id":16,
         "name":"Testss",
         "description":null,

      },
      {  
         "id":18,
         "name":"TestSan",
         "description":null,

      },

      {  
         "id":2,
         "name":"Standard ward",
         "description":"",
         "sharing":4,
         "set_value":2
      }
   ]

4 个答案:

答案 0 :(得分:1)

您可以为此

使用filter()includes()方法
const arryids = {1,3,9};//remove id array 
const filteredarray = this.ViewData.filter(d=> !array.includes(d.id));
this.ViewData= filteredarray;

答案 1 :(得分:0)

使用filter

ViewData = ViewData.filter( s => !dataArray.includes( s.id ) ) 

答案 2 :(得分:0)

filterincludes组合使用。 filter将根据条件返回一个新数组。条件返回true的那些项将添加到输出数组中。

const ids = [21, 23];
const viewData = [  
      {  
         "id":5,
         "name":"Private",
         "description":"",

      },
      {  
         "id":7,
         "name":"Semi Private",
         "description":"",

      },
      {  
         "id":8,
         "name":"laboratory",
         "description":"",

      },
      {  
         "id":15,
         "name":"Test",

      },
      {  
         "id":16,
         "name":"Testss",
         "description":null,

      },
      {  
         "id":18,
         "name":"TestSan",
         "description":null,

      },
      {  
         "id":21,
         "name":"TestBBB",
         "description":"test",

      },
      {  
         "id":23,
         "name":"TestOne",
         "description":null,

      },
      {  
         "id":2,
         "name":"Standard ward",
         "description":"",
         "sharing":4,
         "set_value":2
      }
   ];
   
const filteredData = viewData.filter(item => !ids.includes(item.id));

console.log(filteredData);

答案 3 :(得分:0)

if(condition){
var key = "your_key";
delete json[key];
}