javascript - Array filter with possible undefined properties -
i have array of objects may or not contain property.
so, in order filter it, this:
array = array.filter(v => v.myproperty != undefined); array = array.filter(v => v.myproperty[0] != undefined);
this removes ones myproperty
undefined and, afterwards, ones first element inside myproperty
.
is there way of having single liner prevents me applying filter
array twice?
something like:
array = array.filter((v => v.myproperty || [])[0] != undefined);
what about:
filtered_array = array.filter(element => element.property && element.property[0]);
what condition evaluate left part first. if it's true, try evaluate right part , return result.
wiki
Comments
Post a Comment