-1

I have list that looks like this :

{1: "a", 2: "a", 3: "c", 4: "c"}

I want to check if there is "c" in that list. This is how part of my code looks :

answer= {1: "a", 2: "a", 3: "c", 4: "c"}

answer[indexString as keyof typeof answer] = value;

if ("c" in answer) {
   console.log("true");
}

I don't know why it only check the id 1,2,3,4 value not the string "a","b", "c". Really appreciate if anyone could help me on this

jojo1711
  • 73
  • 7
  • 1
    Does this answer your question? [How to check if a value exists in an object using JavaScript](https://stackoverflow.com/questions/35948669/how-to-check-if-a-value-exists-in-an-object-using-javascript) – Raafat dev Mar 30 '21 at 08:49

3 Answers3

2

What you have is an object, not a list. To check if some vale exist in the values of an object use Object.values function that creates a list of values. And then check if the need value is present in it: Object.values(answer).includes("c")

Teivaz
  • 5,039
  • 4
  • 28
  • 72
2

You need to check if certain value is present in the object. You can do the following,

answer= {1: "a", 2: "a", 3: "c", 4: "c"};

if(Object.values(answer).includes("c")){
 console.log("true");
}
TechySharnav
  • 3,801
  • 2
  • 11
  • 25
1

Did you try using

Object.values(answer).includes("c");