Since I haven't seen an answer using Object.entries
here's one. Note, due to Object.entries()
implementation being significantly slower than Object.keys()
, this will also be slower than the accepted answer, but some may prefer this for readability or extendability (easier to pass a different filtering function).
const acceptedValues = ["value1", "value3"];
const myObject = {
prop1:"value1",
prop2:"value2",
prop3:"value3"
};
const filteredEntries = Object.entries(myObject).filter(([, v]) => acceptedValues.includes(v));
const filteredObject = Object.fromEntries(filteredEntries);
Or as a longish one-liner:
const filteredObject = Object.fromEntries(Object.entries(myObject).filter(([, v]) => accepted.includes(v)));