I have this object array:
var result = [
{
appAddress:"127.0.0.1",
name:"AppServer1",
dbConnection:""
},
{
appAdress:"",
name:"DBServer1",
dbConnection:"Server=.;Database=master;Integrated Security=SSPI"
}];
Now i need to get only the name values (into an array), where the appAddress isn't empty. I tried array.filter() and $.map() but none of these methods seems to do what i want.
This is what i tried:
var appServers = $.map(result, function (val, key) {
return (key == 'appAddress' && val.length > 0) ? key : null;
});
and
var appServers = result.filter(function (entry) {
return entry['displayName'];
});
appAddress, if it's NOT empty,pushit on to a new array that contains the results..filter()and.map()are exactly what you need.$mapis that you expect it to iterate over the properties of each object, but it does not: it iterates over the array.keywill never be"appAddress", because$.mapis operating on an array (which only has key names like0,1,2, etc..), not the objects inside that array. As shown in Matthias's answer, you should use the first argument (val) to access each object andval.appAddress/val.nameto get its property values.