javascriptdocxtemplater

Handle undefined values in docxtemplater or javascript object array


I have an object array that will be exported to word document via DocXTemplater

Sample array

[
    {Name:"jon doe",age:27},
    {Name:"joe roe",age:27,Address:"new jersey"}
]

Now the template will be

{#arrayVarName}{Name},{age},{Address}{/arrayVarName}

This should output,

Jon,27,undefined
Joe,27,new jersey

Now I want to filter all undefined and replace them with either empty string or some custom string, how can this be done in docxtemplater, or can all the undefined in object array be replaced with custom string?


Solution

  • You can now customize this setting globally :

    const doc = new Docxtemplater(zip, {nullGetter() { return ''; }});