Why cant I filter my javascript/jquery object -
i have javascript object array looks this:
[ {"fname":"tim", "lname":"thompson", "user_id": "2", "route_id": "3", "route_day": "wed"}, {"fname":"john", "lname":"wilson", "user_id": "3", "route_id": "3", "route_day": "wed"}, {"fname":"george", "lname":"lane", "user_id": "5", "route_id": "2", "route_day": "mon"} ]
i tried filter out results using filter function
function filtercustomerdata(route,day) { var jsoncustdata = json.parse(localstorage.getitem("db")); console.dir(jsoncustdata); // object array 300+ objects console.log( "route is: " + route + " day is: " + day);// route is: 3 day is: wed var routes = jsoncustdata.filter(function(ele, ind,arr){ return ele.route_id == route && ele.route_day == day; }); console.dir(routes); // array[0] fillcustomerdata(routes); }
but everytime empty array. know must doing wrong, can't figure out what.
edit: again sorry mistake, entered full function time.
- use
==
instead of=
- use
route_day
instead ofroute_day
var objarr = [ {"fname":"tim", "lname":"thompson", "user_id": "2", "route_id": "3", "route_day": "wed"}, {"fname":"john", "lname":"wilson", "user_id": "3", "route_id": "3", "route_day": "wed"}, {"fname":"george", "lname":"lane", "user_id": "5", "route_id": "2", "route_day": "mon"} ]; var newobj = objarr.filter(function(ele, ind, arr){ console.log(ele); return ele.route_id == "3" && ele.route_day == "wed"; }); console.log(newobj);
Comments
Post a Comment