简体   繁体   中英

how to remove null from object property array

I am using ngFor within component. 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:

<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.

 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. Hope it helps you.

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

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

});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM