If my memory serves me right, the situation at hand is as follows :
const keysToRetain = ["var1", "var2", "var3"];
const objectData = {
"var1": 1,
"var2": 2,
"var3": 3
};
If this assumption holds true, then you can implement the following approach :
const filteredResult = Object.entries(objectData)
.filter(item => keysToRetain.includes(item[0]))
.reduce((accumulator, entry)=>{
accumulator[entry[0]] = entry[1];
return accumulator;
}, {});
Alternatively,
const filteredResult = Object.entries(objectData)
.map(item => ({
"key": item[0],
"value": item[1]
})).filter(item => keysToRetain.includes(item.key))
.reduce((accumulator, entry)=>{
accumulator[entry.key] = entry.value;
return accumulator;
}, {});
This method essentially filters out entries in the object based on whether their key matches any of the specified keys, and then compacts them back into an object.