javascript - How to Remove Elements from Array of Objects based on some condition - Stack Overflow

I am facing a critical problem in angularI have the following arraysdataArray (3) [21, 21, 23]and the b

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

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 = [  
      {  
         "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
      }
   ]

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

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 = [  
      {  
         "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
      }
   ]
Share Improve this question asked Mar 12, 2018 at 7:19 SanthoshSanthosh 1,0892 gold badges20 silver badges52 bronze badges 1
  • Possible duplicate of Using array map to filter results with if conditional – Ramesh Rajendran Commented Mar 12, 2018 at 7:25
Add a ment  | 

4 Answers 4

Reset to default 4

you can make use of filter() and includes() method for this

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

Use filter

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

Using filter with bination includes. filter will return a new array based on the condition. Those items for which the condition returns true, will be added in the output array.

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];
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744293037a4567124.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信