Search code examples
javascriptknockout-mapping-plugin

Knockout Mapping Ignore an Array of an Array


I am trying to ignore a property that is an array of an array. How do I specify this in the ignore object?

ko.mapping.fromJS(data, {ignore: 'ArrayA.ArrayToIgnore'}, self);

Solution

  • Options like observe, ignore and copy have limited capabilities when it comes to collections as far as I know. I do not know if there is a more robust solution, but I handled a similar sitution like below a while ago.

    Sample Data (There are journals and each journal has currencies)

                    var data = {
                        "journals": [{
                             "id": 1006,
                             "number": "2017/48",
                             "currencies": [{
                                       "id": 1,
                                       "code": "USD"
                        }]
                    }]
                };
    

    Mapping (We would like to ignore id property of currencies arrays of journals)

                    var mappingOptions = {
                    currencies: {
                        create: function (options) {
                            return ko.mapping.fromJS(options.data, { ignore: ["id"] });
                        }
                    }
                }
    

    Initialization

    var vm = ko.mapping.fromJS(data, mappingOptions);