我想在数组中找到索引。数组中的位置是对象,我想过滤它们的属性。我知道要过滤哪些键及其值。问题是获得符合条件的数组索引。
现在我编写了代码来过滤数据并返回对象数据,但不返回数组索引。
var data = [
{
"text":"one","siteid":"1","chid":"default","userid":"8","time":1374156747
},
{
"text":"two","siteid":"1","chid":"default","userid":"7","time":1374156735
}
];
var filterparams = {userid:'7', chid: 'default'};
function getIndexOfArray(thelist, props){
var pnames = _.keys(props)
return _.find(thelist, function(obj){
return _.all(pnames, function(pname){return obj[pname] == props[pname]})
})};
var check = getIndexOfArray(data, filterparams ); // Want to get '2', not key => val