-1

I need to execute some code. But my code is not working properly. I have made an simple example to demonstrate the malfunction.

When I run

console.log("val" in Object.values({key:"val"})); //returns false

It gives me false. But if I run

console.log(Object.values({key:"val"}))

outputs => ['val']

I don't understand if it is supposed to work like this. If yes. Why ?

Thanks in Advance.....:)

NSK
  • 180
  • 2
  • 12

1 Answers1

1

MDN says "the in operator returns true if the specified property is in the specified object or its prototype chain.", Object.values returns an array. To check if an item exists in an array, use the Array.includes method.

console.log(
  Object.values({key:"val"}).includes("val")
); // Returns true
Luke-zhang-04
  • 782
  • 7
  • 11