【发布时间】:2012-07-24 13:37:03
【问题描述】:
我可能需要编写一个函数来输出数组内对象的索引,显然,在下面的示例中使用 $.inArray 会很好地返回。
array = ['one', 'two', 'three'];
$.inArray('one', array) // 0
使用更精细的数组,如何找到嵌套在其中的对象的索引?
array = [
{
name: 'One', // index??
data: {
title: 'Title One',
content: 'Content One'
}
},
{
name: 'Two',
data: {
title: 'Title Two',
content: 'Content Two'
}
},
{
name: 'Three',
data: {
title: 'Title Three',
content: 'Content Three'
}
}
];
我听说过 $.grep() 方法,indexOf() .. 不确定使用哪一个来返回对象所在索引的整数
【问题讨论】:
-
这些函数都不处理多维数组。您必须推出自己的搜索功能。 indexof/grep 可以在数组的每一层使用,但是在层之间移动取决于你。
标签: javascript arrays indexing