Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.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中获取数组[Array[{JSON}]]中元素的索引_Javascript_Arrays - Fatal编程技术网

如何在Javascript中获取数组[Array[{JSON}]]中元素的索引

如何在Javascript中获取数组[Array[{JSON}]]中元素的索引,javascript,arrays,Javascript,Arrays,我有这样的东西,(…) 我想得到id=2的元素的索引。例如,我尝试了很多方法(使用find,findIndex),但都没有成功。这可能是因为我使用了错误的搜索功能,所以我在此代码中寻求帮助 var输入=[ [{id:0,名称:'test',数据:{},其他:'..'}], [{id:1,名称:'test',数据:{},其他:'..'}], [{id:2,名称:'test',数据:{},其他:'..'}], [{id:3,名称:'test',数据:{},其他:'..'}], [{id:4,名称:'

我有这样的东西,
(…)

我想得到id=2的元素的索引。例如,我尝试了很多方法(使用
find
findIndex
),但都没有成功。这可能是因为我使用了错误的搜索功能,所以我在此代码中寻求帮助
var输入=[
[{id:0,名称:'test',数据:{},其他:'..'}],
[{id:1,名称:'test',数据:{},其他:'..'}],
[{id:2,名称:'test',数据:{},其他:'..'}],
[{id:3,名称:'test',数据:{},其他:'..'}],
[{id:4,名称:'test',数据:{},其他:'..'}]
]
log(input.findIndex(items=>items.some(item=>item.id==2))
var test=“test”;
var myArray=[
[{id:0,名称:test,数据:{},其他:'..'}],
[{id:1,名称:test,数据:{},其他:'..'}],
[{id:2,名称:test,数据:{},其他:'..'}],
[{id:3,名称:test,数据:{},其他:'..'}],
[{id:4,名称:test,数据:{},其他:'..'}]
];
myIndex=myArray.indexOf(myArray.filter(items=>items.some(item=>item.id==2))[0]);

log('myIndex:'+myIndex)在问题中添加您尝试过的内容和遇到的错误。
findIndex
应该可以工作。请注意,您有一个二维数组。因此,在访问
id
之前,您需要获取
0
索引,例如:
arr.findIndex(o=>o[0].id==2)
您的示例(数组)中没有更新我的答案。如果没有帮助,请告诉我。更新我的答案。如果没有用,请告诉我
json
[
 [{id:0, name:test, data:{},other:'...'}],
 [{id:1, name:test, data:{},other:'...'}],
 [{id:2, name:test, data:{},other:'...'}],
 [{id:3, name:test, data:{},other:'...'}],
 [{id:4, name:test, data:{},other:'...'}]
]