Jim Platts Jim Platts - 1 month ago 17
jQuery Question

how to grep this Javascript object array?

I have the following object structure:

var mapData =
{
Summary:
{
ReportName: 'Month End Report'
},
NortheastRegion:
{
Property1: 123,
RegionName: 'Northeast'
},
SoutheastRegion:
{
Property1: 456,
RegionName: 'Southeast'
},
}


I want to write a grep function that returns an array of region names. The following function is not returning any values:

var regions = $.grep(mapData, function(n,i)
{
return n.RegionName;
});


What am I missing here?

Answer

Just turn $.grep to $.map and you would good to go.

var regions = $.map(mapData, function(n,i)
{
   return n.RegionName;
});
Comments