0

I have a function that accepts two args - an object, and a value. I want to check if the object contains the given value.

For example:

{a: "b", c: "d"}

Contains value "d" as key c stores it.

Konrad Borowski
  • 10,745
  • 3
  • 55
  • 71
Joseph Romero
  • 55
  • 1
  • 1
  • 5
  • `Jim` isn’t an own property. Only `name` and `course` are, in this case. That’s what the [`hasOwnProperty`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/hasOwnProperty) method is for. – Sebastian Simon Feb 02 '18 at 00:27
  • *hasOwnProperty* tests if an object has a property, you're supposed to be testing values. – RobG Feb 02 '18 at 00:27

1 Answers1

1

You can use Object.values to get the values of the object and check if the value passed is in them using includes.

var containsValue = function(val, obj){
  return Object.values(obj).includes(val);
};

console.log(containsValue("Jim", {name: "Jim", course: "FEDev"}));
console.log(containsValue("Jim", {name: "Nope", course: "FEDev"}));
mdatsev
  • 2,886
  • 10
  • 25