Select to view content in your preferred language

Can you use "filter" on an array with esri javascript 3.5 api

593
2
09-03-2013 10:30 AM
LuciHawkins
Frequent Contributor
We are using the javascript version for Arcgis 10.1 "Public Notification" template with our data and would like filter out "null" or empty data responses when it gathers the field data for the Avery labels or the CSV output file.  I attempted to use some code and it either gives me an "undefined" error in a dialog box when I select avery label or "strCsvParam.filter is not a function" error in a dialog box when I select CSV format. 

The main problem is that we have parcels that have "Owner Name", "Address1", "Address2", "Address3", "City", "State", "Zip" for fields.  Not all of the parcels with have any data in "Address2" or "Address3" which leaves blank lines between "Address1" and "City, State, Zip" on the avery label.

I just want to remove the null or empty data from the array.

This is the code that I have tried to remove the empty fields:

strAveryParam = strAveryParam.filter(function(){return true; });


Any advice on how to remove empty, null or false values from an array?

Thanks,

Luci
0 Kudos
2 Replies
JasonZou
Frequent Contributor
Assuming strAveryParam is a string array,

strAveryParam = dojo.filter(strAveryParam, function(aAveryLabel) { return !!aAveryLabel; });


If strAveryParam is an object array that contains a property "label" that will be used for the filter.

strAveryParam = dojo.filter(strAveryParam, function(oAvery) { return !!oAvery.label; });
0 Kudos
JeffJacobson
Frequent Contributor
If you're using AMD style, you'll want to modify Jason's sample to use dojo/_base/array.filter() instead of dojo.filter.

If you don't need to support IE versions older than 9 then you should use the built in JavaScript Array filter method instead of either of the dojo methods. (This is for performance reasons.)
0 Kudos