I need to find the fastest way to remove all $meta
properties and their values from an object, for example:
{
"part_one": {
"name": "My Name",
"something": "123",
"$meta": {
"test": "test123"
}
},
"part_two": [
{
"name": "name",
"dob": "dob",
"$meta": {
"something": "else",
"and": "more"
}
},
{
"name": "name",
"dob": "dob"
}
],
"$meta": {
"one": 1,
"two": 2
}
}
Should become the following given that the $meta
property could be at any point in the object so some form of recursion will probably be needed.
{
"part_one": {
"name": "My Name",
"something": "123"
},
"part_two": [
{
"name": "name",
"dob": "dob"
},
{
"name": "name",
"dob": "dob"
}
]
}
Any help or advice would be greatly appreciated!
Thank you!
(Apologies, I do not yet have enough reputation points to comment directly.)
Just FYI, typeof null === 'object', so in the removeMeta() example offered by @joseph-marikle, the function will recurse on a null value.
Read more here: why is typeof null "object"?
Here is a function that takes either a string or an array of strings to remove recursively (based on Joseph's answer):
A simple self-calling function can do it.
As @floor commented above:
JSON.parse(JSON.stringify(obj, (k,v) => (k === '$meta')? undefined : v))
Using some form of
delete objectName.$meta
should send you in the right directionHow do I remove a property from a JavaScript object?