Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/370.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
Javascript 你能找到多深?去吧_Javascript_Arrays_Reactjs_Find - Fatal编程技术网

Javascript 你能找到多深?去吧

Javascript 你能找到多深?去吧,javascript,arrays,reactjs,find,Javascript,Arrays,Reactjs,Find,我知道我可以很容易地找到像 answers.find(question => question.questionId === displayingQuestionId) 但我的结构是: answers: [ {questionId: 0000, answers: [{title: 'answer1'},{title: 'answer2'}] ] 因此,我如何使用find: 我想在这里找到与答案的问号相匹配的问号 我意识到这相当令人困惑 answers.find(问题=>que

我知道我可以很容易地找到像

answers.find(question => question.questionId === displayingQuestionId)
但我的结构是:

answers: [
  {questionId: 0000,
   answers: [{title: 'answer1'},{title: 'answer2'}]
]
因此,我如何使用find:


我想在这里找到与答案的问号相匹配的问号

我意识到这相当令人困惑

answers.find(问题=>question.answers.questionId===显示问题ID)


类似于^但不起作用…

所以基本上你可以得到你想要的深度。但是,您不能仅使用question.answers.questionId进行正确的比较。 显然,.find的每个循环都将为您提供顶级对象。之后你就可以随心所欲了

例如:

let arr = [{'questionId': 0000, 'answers': [{'title': 'answer1'},{'title': 'answer2'}]}];

//returns object of the top level if questioned equals to 0

arr.find((answer) => answer.questionId === 0);

//comparison with the internal array elements
//returns answer if one of the internal answers has title 'answer 1'

arr.find((answer) => !!answer.answers.find((internalAnswer => internalAnswer.title === 'answer1')));

第一个选项适用于您的结构,除非您希望匹配其中一个答案中的属性。答案或您尚未显示完整的数据深度
find
可能不是执行此操作的函数,因为即使您可以使用检查内部数组的条件,它也只能从顶层数组返回元素。“我想找到与答案的questionId相匹配的questionId”你能举个例子说明你想要实现什么吗?显示你真正想要搜索的对象。你显示了一个有答案的对象,但它们只有标题。你在找什么?