简体   繁体   English

如何从对象属性数组中删除null

[英]how to remove null from object property array

I am using ngFor within component. 我在组件内使用ngFor。 I have the following object. 我有以下目的。

{"name":"name_text","values":[null,{"id":1,"text":"text1"},{"id":2,"text":"text2"},{"id":3,"text":"text3"},null,{"id":5,"text":"text5"},{"id":6,"text":"text6"},{"id":7,"text":"text7"}]}

this is my ngFor in component: 这是我在组件中的ngFor:

<option *ngFor="let val of enumeration.values" value="{{val}}">{{val["text"]}}</option>

Problem is that there are null objects in array so i am unable to run it. 问题是数组中有空对象,所以我无法运行它。 Can somebody help me and give me some hints how can i remove them? 有人可以帮助我,并给我一些提示如何删除它们吗?

Thanks 谢谢

Use Array#filter to get rid of the null entries and modify the original object. 使用Array#filter摆脱null条目并修改原始对象。

 var obj = {"name":"name_text","values":[null,{"id":1,"text":"text1"},{"id":2,"text":"text2"},{"id":3,"text":"text3"},null,{"id":5,"text":"text5"},{"id":6,"text":"text6"},{"id":7,"text":"text7"}]}, res = obj.values.filter(v => v); obj.values = res; console.log(obj); 

Use the jquery grep function. 使用jquery grep函数。 Hope it helps you. 希望对您有帮助。

YourArray = jQuery.grep(YourArray, function(n, i){ YourArray = jQuery.grep(YourArray,function(n,i){

return (n != "" && n!= null); return(n!=“” && n!= null);

}); });

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

相关问题 如果值为空,未定义或为空,如何从对象和/或数组中删除属性? - How to remove a property from an object and/or array if the value is null, undefined or empty? 如何从属性等于Null的数组中删除对象-Lodash - How to Remove Object From Array Where Property Equals Null - Lodash 如何将对象属性复制到其他对象,然后从数组中删除该对象? - How to copy object property to other object and then remove this object from array? 如果对象中的属性不存在,如何从数组中删除对象 - How to remove object from array if property in object do not exist 如何通过对象的属性从对象数组中删除特定的对象? - How to remove a specific Object from an array of Objects, by object's property? 如何从所有数组对象中删除对象属性并通过 api 传递 - How to remove and object property from all array object and and pass by api 如何从数组中的 object 中删除属性? - How can I remove a property from an object in an array? 如何从 Mongoose 中的数组中删除 object 的属性? !重要的 - How can I remove a property of object from an array in Mongoose? !Important 如何从数组对象属性值中删除重复项? - How to remove duplicates from an array object property value? 如何通过jQuery删除对象属性并从数组中获取特定值 - how to remove object property and take specific values from array by jQuery
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM