Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Reactjs:如何检查列表中是否存在数据_Reactjs - Fatal编程技术网

Reactjs:如何检查列表中是否存在数据

Reactjs:如何检查列表中是否存在数据,reactjs,Reactjs,我的列表如下所示: {1:“a”,2:“a”,3:“c”,4:“c”} 我想检查一下列表中是否有“c”。以下是我的部分代码的外观: answer= {1: "a", 2: "a", 3: "c", 4: "c"} answer[indexString as keyof typeof answer] = value; if ("c" in answer) { console.log(&qu

我的列表如下所示:

{1:“a”,2:“a”,3:“c”,4:“c”}

我想检查一下列表中是否有“c”。以下是我的部分代码的外观:

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

answer[indexString as keyof typeof answer] = value;

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

我不知道为什么它只检查id 1,2,3,4值,而不是字符串“a”、“b”、“c”。如果有人能在这方面帮助我,我将不胜感激。

您所拥有的是一个对象,而不是列表。要检查对象的值中是否存在某些值,请使用创建值列表的函数。然后检查其中是否存在需要的值:
Object.values(answer).includes(“c”)
您需要检查对象中是否存在特定的值。您可以执行以下操作:

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

if(Object.values(answer).includes("c")){
 console.log("true");
}
你试过使用

Object.values(答案)。包括(“c”)


这回答了你的问题吗?