Search code examples
knockout.jsknockout-mapping-plugin

KnockoutJS Mapping toJS ignore nested keys


Can I not ignore nested properties when using toJS in KnockoutJS' mapping plugin?

Example:

var obj = {
    "akey": {
        "anestedkey": "ignore",
        "anotherkey": "value"
    }
};

console.log(ko.mapping.toJS(obj, {
    "ignore": ["akey.anestedkey"],
}));

Expected output is

{
    akey: {
        anotherkey: "value"   
    }
}

Actual output is

{
    akey: {
        anestedkey: "ignore"
        anotherkey: "value"   
    }
}

JSFiddle: http://jsfiddle.net/48KVU/


Solution

  • It works if you remove the parent (if you know your key is unique in obj or if you want to remove all occurences):

    var obj = {
        "akey": {
            "anestedkey": "ignore",
            "anotherkey": "value"
        }
    };
    
    console.log(ko.mapping.toJS(obj, {
        "ignore": ["anestedkey"], //here
    }));
    

    http://jsfiddle.net/GabrielTran/48KVU/1/