I have an array of objects like this:
var a = [{'id': 1}, {'id':''}, {'id':3}]
And now I need to count the array except the empty id so the answer should be 2.
I tried to used filter function but I couldn’t get my expected out put.
This is the codes I am done so far.
a.filter(Boolean).length; // Returns 3. It should be 2.
Any idea is much appreciated. Thank you
Try using reduce.
array.filter(Boolean).length
is returns 3 because filter returns elements if provider returns true, In your case, passing object toBoolean
({…}) which returns true, so it keeps all the elements.