Basically, I have JavaScript array that looks like this:
var keyFilters = ["key_1", "key_2", "key_3"];
And I have an object that looks like this:
myObject["key_1"] = "Value 1";
myObject["key_2"] = "Value 2";
myObject["key_random"] = "Value 2";
I need to pair down my myObject
object to only have the keys that exists in the keyFilters
array. Sometimes, the object won't have a key that exists in the filter. In this example, filtering myObject
against keyFilters
would result in this outputted object:
myObject = {
"key_1": "Value 1",
"key_2": "Value 2",
}
I know there is no map
function for objects in JS, but there is one for arrays. Would it be best to use that or just write a single-off function for iterating over the array and then over the object, and push to a new object that only has the matching keys, or is there some slicker way?