[英]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.