简体   繁体   English

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

[英]How to Remove Elements from Array of Objects based on some condition

I am facing a critical problem in angular 我面临着一个严重的角度问题

I have the following arrays 我有以下数组

dataArray (3) [21, 21, 23]

and the below is actual Data from API 以下是来自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
      }
   ]

I have to process ViewData (2nd array) after processing ViewData should not contain JSON Objects which ids are present in dataArray(I mean if the any id of ViewData is equal to id which is in DataArray i want to remove that JSON Object from View Data) (Out put should be like below ) 我必须在处理ViewData后不应该包含在dataArray中存在ID的JSON对象来处理ViewData(第二个数组)(我的意思是如果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
      }
   ]

you can make use of filter() and includes() method for this 您可以为此使用filter()includes()方法

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

使用filter

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

Using filter with combination includes . 结合使用includes filter filter will return a new array based on the condition. filter将根据条件返回一个新数组。 Those items for which the condition returns true, will be added in the output array. 条件返回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); 

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何过滤对象数组以根据 MongoDB 聚合中的条件删除元素? - How to filter an array of objects to remove elements based on condition in MongoDB aggregate? 如何从满足条件的数组中删除元素 - How to remove elements from array satisfying condition Mongoose:如果满足条件,则从 Mongo 中的对象数组中删除元素 - Mongoose: Remove elements from array of objects in Mongo if condition is met 根据条件从嵌套的对象数组中删除项目 - Remove items from nested array of objects based on a condition 如何从对象数组中删除元素 - How to remove elements from an array of objects Append 值根据某些条件转换为对象数组? - Append values into array of objects based on some condition? 如何根据Typecript中的某个给定数字对对象数组的元素进行分组 - How to group elements of an array of objects based on some given number in Typecript 数组操作 - 如何根据条件从数组中删除特定条目 - array manipulation - How to remove a specific entry from array based on a condition 如何从对象数组中删除满足条件的object? - how to remove the object that meets the condition from an array of objects? 根据属性值过滤对象数组,并从剩余对象中删除一些属性 - Filter array of objects based on property value, and remove some properties from remaining objects
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM