0

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

1
  • Try to do first filter the array and then use the ngFor <option *ngFor="let val of enumeration.values.filter(val => val !== null)" value="{{val}}">{{val["text"]}}</option> Commented Apr 24, 2017 at 12:17

2 Answers 2

2

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);

Sign up to request clarification or add additional context in comments.

7 Comments

Nice use of .values()
this.enumeration = data.get(enumName); this.enumeration.values = this.enumeration.values.filter(v => v); says there is no such a property "filter"
@MartinFric There's no such property 'filter' or 'filter' is not a function?
property "filter" does not exist on type "Object". cause enumeration is object which has property as array of objects. maybe i need to remove array from object filter it and then rewrite to object?
@MartinFric Try to assign the filtered array to a newly made res variable and then assign it to the values array (look at my edit).
|
0

Use the jquery grep function. Hope it helps you.

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

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

});

2 Comments

dont want to add jquery to my node modules.
If that is the case, please try filtering your array in server side itself so that, you will not have to bother about null values in client side.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.