What is the best way to filter an object this way in ES6?
Starting data:
const acceptedValues = [\"value1\",\"value3\"]
const myObject = {
prop1:
Using a simple for loop and get object by key.
const acceptedValues = ["value1","value3"]
const myObject = {
prop1:"value1",
prop2:"value2",
prop3:"value3"
}
Object.prototype.getKeyByValue = function( value ) {
for( var prop in this ) {
if( this.hasOwnProperty( prop ) ) {
if( this[ prop ] === value )
return prop;
}
}
}
for (var i in acceptedValues) {
if (myObject.getKeyByValue(acceptedValues[i])){
console.log(acceptedValues[i]);
}
}