简体   繁体   English

在淘汰赛js中使用映射选项时,如何跳过项目而不包括它?

[英]How to skip an item and not include it when using mapping options in knockout js?

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. 当我使用ko.mapping.fromJS实用程序填充数组时,我只想包含和返回将selected属性设置为true的项目。

I have written the following code but it is population the array with undefined if the items don't have the selected property. 我已经编写了以下代码,但是如果项目没有selected属性,它将使用未定义的数组进行填充。

    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: 老实说,最好先过滤JS数据:

var selectedMedia = ko.utils.arrayFilter(ko.toJS(data.Medias), function(item) {
        return item.Selected;
    });
self.Medias = ko.mapping.fromJS(ko.toJS(data.Medias), mappingOptions);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM