javascript - Find index in array of objects -
i find index in array. positions in array objects, , want filter on properties. know keys want filter , values. problem index of array meets criteria.
for made code filter data , gives me object data, not index of array.
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 '2', not key => val
here thefiddle hope helps you
for(var intindex=0;intindex < data.length; intindex++){ eachobj = data[intindex]; var flag = true; (var k in filterparams) { if (eachobj.hasownproperty(k)) { if(eachobj[k].tostring() != filterparams[k].tostring()){ flag = false; } } } if(flag){ alert(intindex); } }
Comments
Post a Comment