Javascript 异常:response.findIndex不是函数
从api响应中,我希望在不使用forEach的情况下获得indexJavascript 异常:response.findIndex不是函数,javascript,Javascript,从api响应中,我希望在不使用forEach的情况下获得indexresultsarray。我在下面找到了ES2016解决方案,但当我运行以下JavaScript时,我得到了 异常:response.findIndex不是函数 JavaScript console.log( response.findIndex(x => x.results.id === 5 ) ); 响应 { "count": 9, "results": [ { "id": 8,
results
array。我在下面找到了ES2016解决方案,但当我运行以下JavaScript时,我得到了
异常:response.findIndex不是函数
JavaScript
console.log( response.findIndex(x => x.results.id === 5 ) );
响应
{
"count": 9,
"results": [
{
"id": 8,
"name": "Example 1"
},
{
"id": 9,
"name": "Example 2"
}
....
]}
findIndex
需要数组,但您的响应是一个对象。但是名为results
的属性是一个数组
转换这个
response.findIndex(x => x.results.id === 5 )
对此
response.results.findIndex(x => x.id === 5 )
它应该是
response.results.findIndex(x=>x.id==5)
@NavneilNaicker谢谢你的指点。你有一个鹰眼伙伴:)