I am using some of the Knockout utility functions described brilliantly here: http://www.knockmeout.net/2011/04/utility-functions-in-knockoutjs.html
I want to do an arrayMap to select certain properties based on a condition, e.g.
return ko.utils.arrayMap(myObservableArray(), function (item) {
return item.Label;
});
Say for example this produces the following output:
[null, , SomeLabel, null, SomeOtherLabel]
I want to select the properties based on a condition, so I try:
return ko.utils.arrayMap(myObservableArray(), function (item) {
if (item.Label && item.Label !== ) {
return item.Label;
}
});
However then you end up with an array like:
[undefined, undefined, SomeLabel, undefined, SomeOtherLabel]
I've also tried this:
return ko.utils.arrayMap(myObservableArray(), function (item) {
return (item.Label && item.Label !== ) ? item.Label : false;
});
but you get:
[false, false, SomeLabel, false, SomeOtherLabel]
So I am then having to do:
var itemsWithLabels = ko.utils.arrayFilter(myObservableArray(), function (item) {
return (item.Label && item.Label !== );
});
return ko.utils.arrayMap(itemsWithLabels, function (item) {
return item.Label;
});
Which will give me:
[SomeLabel, SomeOtherLabel]
Is there a more efficient way of accomplishing this, in one shot, using ko.utils or similar?