I'm trying to filter down an array of objects. But the property of the object to filter on is defined elsewhere as a variable and not 100% how i can construct the dot-notation syntax.
So i have a array object like this
var listings = [
{"id" : "1", "name":"a name", "surname" : "a surname"},
{"id" : "2", "name":"b name", "surname" : "b surname"},
{"id" : "3", "name":"c name", "surname" : "c surname"}
]
I have a variable the specifies what property of the object to filter on. This variable isn't always set to name. In the example above it could be on id,name or surname.
var filter = 'id'
A var for the filter val to look for
var filterVal = '2'
Then i have this grep call.
listings = $.grep(listings, function (obj, index) {
return obj.id === filterVal;
});
But instead of having obj.id hard coded I want something like:
obj."value of filter variable" === filterVal
Thanks in advance
Ben