I would like to only include and return items that have the selected property set to true when I am populating my arrays using the ko.mapping.fromJS utility.
I have written the following code but it is population the array with undefined if the items don't have the selected property.
var mappingOptions = {
create: function (options) {
if (options.data.Selected) {
var item = ko.mapping.fromJS(options.data);
return item;
}
}
};
self.Medias = ko.mapping.fromJS(ko.toJS(data.Medias), mappingOptions);
the produced array should not have the undefined elements.
[Object, undefined, Object, undefined, Object, undefined, Object]
To be honest, you're better off filtering the JS data first:
var selectedMedia = ko.utils.arrayFilter(ko.toJS(data.Medias), function(item) {
return item.Selected;
});
self.Medias = ko.mapping.fromJS(ko.toJS(data.Medias), mappingOptions);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.