My question is pretty simple actually but I couldnt find satisfied answer
var ages = [3, undefined, undefined, 20];
lets say that I have array like above.And I wanna get the max value in array which is 20.
this is what I did but returned undefined so I tried something like that below and worked
but filtering all undefined elements(iterating all) and then finding biggest element is big cost.I need something built-in js function such as Math.max.apply which can skip undefined elements and find the biggest value from array.
As far as I understand your question, I would like to answer it.
I have an idea to do this.
var values = [3, undefined, 5, 6, 4, undefined, 20]; values.sort(); console.log(values);
This results me with max value – 20.
EDIT: Now this can work on negative values too.
var values = [3, undefined, 5, 6, 4, undefined, 20]; values.sort((a,b) => b-a); console.log(values);