Consider having a complex nested JavaScript object, for example:
{
"?xml": {
"@version": "1.0",
"@encoding": "UTF-8"
},
"Customer": {
"@xmlns": "http://NamespaceTest.com/CustomerTypes",
"@xmlns:xsi": "http://www.w3.org/2001/XMLSchema-instance",
"Name": {
"#text": "Name1"
},
"DeliveryAddress": {
"Line1": {
"@xmlns": "http://NamespaceTest.com/CommonTypes",
"#text": "Line11"
},
"Line2": {
"@xmlns": "http://NamespaceTest.com/CommonTypes",
"#text": "Line21"
}
}
}
}
I want to specify certain properties I wish to remove from the structure using an array, such as ["?xml", "@xmlns"]
, resulting in the output below:
{
"Customer": {
"@xmlns:xsi": "http://www.w3.org/2001/XMLSchema-instance",
"Name": {
"#text": "Name1"
},
"DeliveryAddress": {
"Line1": {
"#text": "Line11"
},
"Line2": {
"#text": "Line21"
}
}
}
}
I'm aware of achieving this using JSON.stringify()
, like so:
function replacer(key, value) {
if (key === "?xml" || key === "@xmlns") {
return undefined;
}
return value;
}
var filtered = JSON.parse( JSON.stringify( original, replacer ) );
However, I am looking for a method that can filter a data structure similar to JSON.stringify()
, but returns an object instead of a string. Is there such a function available?