I have this object:
{
"id": "33343232",
"createdAt": "2022-07-26T13:44:01.080Z",
"updatedAt": "2022-07-26T13:45:31.000Z",
"name": "Name Here",
"description": "text",
}
and another object is :
specificFeatures": {
"id": "33343232",
"createdAt": "2022-07-26T13:44:01.087Z",
"updatedAt": "2022-07-26T13:45:31.000Z",
"name": "Name Here",
"description": "text",
"coverage": "international",
"income": 0,
"observationIncome": "",
}
now, I want the property keys of the specificFeatures
object that are the same as the first object to be deleted.
In this example, it would be:
specificFeatures": {
"coverage": "international",
"income": 0,
"observationIncome": "",
}
Can you tell me how can I do this?
2
Answers
This can be achieved in a simple one-liner:
The above converts the object into an array of entries, then filters the keys against
myObj
before converting back into an Object.To achieve that, you can simply iterate over the object properties, check whether the "original" object has the property, and if so, delete it from "specificFeatures" object:
Pay attention that this way you mutate the original object, and in case you need to keep the original object as is, a new unique copy should be created. One of the possible solutions could be a combination of Object.entries() and Array.reduce() methods: