Hide Table of Contents
esri/dijit/util
esri/layer/pixelFilters
esri/process
esri/support
esri/workers
Object: esri/lang

require(["esri/lang"], function(esriLang) { /* code goes here */ });

Description

(Added at v3.8)
Utility methods for working with strings, arrays and objects.

When coding legacy (non-AMD) style, there is no need to require the module. All methods and properties are available in the namespace. For example, esri.filter().

Samples

Search for samples that use this class.

Methods

NameReturn typeSummary
filter(object, callback, thisObject?)ObjectCreates a new object with all properties that pass the test implemented by the filter provided in the function.
isDefined(value)BooleanReturns true when the value is neither null or undefined.
stripTags(value)Object | StringStrips HTML tags from a String or Object.
substitute(data, template?, options?)StringA wrapper around dojo.string.substitute that can also handle wildcard substitution.
valueOf(array, value)ObjectIterates through the argument array and searches for the identifier to which the argument value matches.
Method Details

filter(object, callback, thisObject?)

Creates a new object with all properties that pass the test implemented by the filter provided in the function.
Return type: Object
Parameters:
<Object> object Required Object to filter.
<Function> callback Required Function or string implementing the filtering.
<Object> thisObject Optional Optional object used to scope the call to the callback.
Sample:
require([
  "esri/lang", "dojo/dom", ... 
], function(esriLang, dom, ... ) {
  function showResults(results) {
    var filterFunction = function(value) {
      if ((value !== ' ') & (Number(value) !== 0)) {
        return true;
      };
    };

    var filteredResults = esriLang.filter(results.features[0].attributes, filterFunction);
    var s = "";
    for (att in filteredResults) {
      s = s + "<b>" + att + ":</b>  " + filteredResults[att] + "<br />";
    };
    dom.byId("info").innerHTML = s;
  };
  ...
});

isDefined(value)

Returns true when the value is neither null or undefined. Otherwise false.
Return type: Boolean
Parameters:
<Object> value Required The value to test. 

stripTags(value)

Strips HTML tags from a String or Object. This method modifies the Object used as input and does not return a new value. If a String is used as input, a new String is returned. (Added at v3.13)
Return type: Object | String
Parameters:
<Object | String> value Required Object or String to be stripped of HTML tags.
Sample:
require(["esri/lang", "dojo/domReady!"], function(esriLang) {
  var myobj = {
    name: "<b>Jane</b>",   //myobj.name = "<b>Jane</b>"
    last: "Doe",
    age: "30"
    };
          
  esriLang.stripTags(myobj);
  console.log(myobj);   //myobj.name = "Jane"
});

substitute(data, template?, options?)

A wrapper around dojo.string.substitute that can also handle wildcard substitution. A wildcard uses the format of ${*}. If no template is provided, it is assumed to be a wildcard. This method is useful if you are not using Graphic or an InfoTemplate, but you want to embed result values in HTML, for example.
Return type: String
Parameters:
<Object> data Required The data object used in the substitution.
<String> template Optional The template used for the substitution. Can be any valid HTML. If no template is included, the wildcard template is used.
<Object> options Optional Object containing a format object used in the substitution.
Object Specifications:
<options>
<Object> format Optional This object contains key:value pairs where the key is either a
  • field name or
  • user-defined identifier.
The value object supports formatType property. This can be one of the following types:
  • NumberFormat, supported by dojo.number.__FormatOptions, or
  • DateFormat, supported by dojo.date.locale.__FormatOptions.
Sample:
Require this method:
require(["esri/lang", ... ], function(esriLang, ... ) { ... });
Example 1: no wildcard is included but is assumed.
esriLang.substitute({state_name: "Arizona", state_capital: "Phoenix"});
Returns: state_name = Arizona state_capital = Phoenix

Example 2: no wildcard is included and only the first property is displayed.
esriLang.substitute({state_name: "Arizona", state_capital: "Phoenix"},null,true);
Returns: state_name = Arizona

Example 3: Format is included for display.
var data = { a: 123456, b: 1000000 };
// The first example shows the key as a field name
esriLang.substitute(data, "Number = ${fieldA}, Date = ${fieldB}", {
  format: {
    fieldA: {
      formatType: "NumberFormat",
      places: 2
    },
    fieldB: {
      formatType: DateFormat",
      datePattern: "y",
      selector: "date"
    }
  }
});
// The second example shows the key as a user-defined identifier specified beside the field name in the template string
esriLang.substitute(data, "Year = ${fieldB:yearOnly}, Full Date = ${fieldB:fullDate}", {
  format: {
    yearOnly: { 
      formatType: "DateFormat", 
      datePattern: "y", 
      selector: "date" 
    },
    fullDate: { 
      formatType: "DateFormat", 
      datePattern: "M/d/y",
      timePattern: "h:mm:ss a", 
      selector: "date and time" 
    }
  }
});
Returns: First returns Number = 123,456.00, Date = 1969 whereas the second example returns Year = 1969, Full Date = 12/31/1969, 7:16:40 PM (this reflects the returned values at date/time of documention).

valueOf(array, value)

Iterates through the argument array and searches for the identifier to which the argument value matches. Returns null if no matching identifier is found.
Return type: Object
Parameters:
<Array> array Required The argument array for testing.
<Object> value Required The value used in the search. If the value is a String, the value is case sensitive.
Sample:

Example 1:

require([
  "esri/lang", ... 
], function(esriLang, ... ) {
  esriLang.valueOf([firststring: "Oregon", secondstring: "Washington"], "Washington");
  ...
});

Returns: secondstring

Example 2:

require([
  "esri/lang", ... 
], function(esriLang, ... ) {
  esriLang.valueOf([firststring: "Oregon", secondstring: "Washington"], "Virginia");
  ...
});

Returns: null

Show Modal