I am trying to remove specific items from a JSON object using an array in JavaScript:
const addressNonRequired = ["addr_linkid_usr", "addr_created", "addr_updated"]
Instead of using the 'delete' method individually for each item, I want to use the above array. How can I achieve this?
// remove non-required data
addressesFound.forEach(row => (
// delete row.addr_linkid_usr,
// delete row.addr_created,
// delete row.addr_updated
addressNonRequired.forEach(item => (
delete row[item];
));
));
I have tried different approaches but still struggling to make it work. Maybe I am missing something...?
Here is the sample input array:
[
{
"addr_id": "41d86d46-8b19-4f4e-be03-f9915ef4947b",
"addr_type": "postal",
"addr_linkid_usr": "user1",
"addr_created": "2021-03-10",
"addr_updated": "",
"addr_active": true,
"addr_postal_as_residential": false,
"addr_international": false,
"addr_autocomplete_id": null
},
{
"addr_id": "b18c2ca6-29cf-4114-9067-b37fd3394638",
"addr_type": "residential",
"addr_linkid_usr": "user1",
"addr_created": "2021-03-10",
"addr_updated": "",
"addr_active": true,
"addr_postal_as_residential": true,
"addr_international": true,
"addr_autocomplete_id": "string"
}
]
The expected output should be:
[
{
"addr_id": "41d86d46-8b19-4f4e-be03-f9915ef4947b",
"addr_type": "postal",
"addr_active": true,
"addr_postal_as_residential": false,
"addr_international": false,
"addr_autocomplete_id": null
},
{
"addr_id": "b18c2ca6-29cf-4114-9067-b37fd3394638",
"addr_type": "residential",
"addr_active": true,
"addr_postal_as_residential": true,
"addr_international": true,
"addr_autocomplete_id": "string"
}
]