javascript - Filter an array based on an object property -
this question has answer here:
i have array of objects, follows:
var events = [ { date: "18-02-2016", name: "event a" }, { date: "22-02-2016", name: "event b" }, { date: "19-02-2016", name: "event c" }, { date: "22-02-2016", name: "event d" } ];
and have date, example "22-02-2016". how can array object date same given date? in example events b , d.
you use array's filter()
function:
function filter_dates(event) { return event.date == "22-02-2016"; } var filtered = events.filter(filter_dates);
the filter_dates()
method can standalone in example reused, or inlined anonymous method - totally choice =]
a quick / easy alternative straightforward loop:
var filtered = []; (var = 0; < events.length; i++) { if (events[i].date == "22-02-2016") { filtered.push(events[i]); } }
Comments
Post a Comment