我有一个像这样的数组:
x=[{id:1}, {id:2}]
并且我想在 filter
函数中访问元素的 id
:
x.filter(function(index) {
if( x[index]['id'] == 1)
return true;
return false;
}
但是说x[index]未定义
我应该如何访问filter
函数中的x[index]
?
最佳答案
你必须这样做:
x.filter(function(item,index) {
if( item.id == 1)
return true;
return false;
});
第一个参数是数组的项,第二个参数是数组的索引。
fiddle :
关于javascript - 如何访问 jQuery 过滤器函数中的元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25637846/