How would I use white-list of fields and a replace function at the same time when using json.stringify?
explains how to use a field list.
Has an answer for filtering null values: https://stackoverflow.com/a/41116529/1497139
Based on that code snippet i am trying:
var fieldWhiteList=['','x1','x2','children'];
let x = {
  'x1':0,
  'x2':null,
  'x3':"xyz", 
  'x4': null,
   children: [
     { 'x1': 2, 'x3': 5},
     { 'x1': 3, 'x3': 6}
   ]
}
function replacer(key,value) { 
  if (value!==null) {
    if (fieldWhiteList.includes(key)) 
      return value;
  }
}
console.log(JSON.stringify(x, replacer,2));And the result is:
{
  "x1": 0,
  "children": [
    null,
    null
  ]
}
Which is not what I expected. I would have expected the x1 values for the children to show up and not null values.
How could i achieve the expected result?
see also jsfiddle
