Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

The operators in this section are valid for maps. Many of these operators take an argument that is a hash path. A hash path is an array whose elements represent the key values (for a map) or array indices of a path from the root of a complex data structure to the element of interest. For example, consider the following data structure:

Code Block
themeConfluence
languagejavascript
{ "colors" : "many",

...


"pi" : [3,1,4,1,5,6,9],

...


"foo"

...

{"bar" : {10:"I like cheese"}}

...


}

The hash path ["foo", "bar"] references the following map inside the data structure:

Code Block
themeConfluence
languagejavascript
{"bar" : {10:"I like cheese"}}

 

delete()

The delete()operator  operator takes a hash path argument designating the member of the original data structure that should be deleted. The delete() operator returns a new data structure with the specified member deleted. If the path is invalid, nothing is deleted and the new data structure is identical to the original. For example:anchor

Code Block

...

themeConfluence
languagejavascript
a = { "colors" : "many",
      "pi" : [3,1,4,1,5,6,9],

...


      "foo" : {"bar" : {10:"I like cheese"}}

...


    };

...


c = a.delete(["foo", "bar", 10])

...


// c = { "colors" : "many",

...


//       "pi" : [3,1,4,1,5,6,9],

...


//       "foo" : {}

...


// }

encode()

The encode() operator returns a string containing the JSON representation of the data structure to which it is applied. For example:

Code Block
themeConfluence
languagejavascript
a = {"a" : 10, "b" : [1, 3, "hello"]};

...


b = a.encode() // b = "{'a' : 10, 'b' : [1, 3, 'hello']}";

keys()

The keys()operator returns the keys of the map to which it is applied. Without an argument, the keys at the top level of the map are returned. The operator also accepts a hash path argument. For example:

Code Block
themeConfluence
languagejavascript
a = { "colors" : "many",

...


      "pi" : [3,1,4,1,5,6,9],

...


      "foo" : {"bar" : {10: "I like cheese", "a" : 12}

...

}
    };

...


b = a.keys();               // b = ["colors", "pi", "foo"]

...


c = a.keys(["foo", "bar"]); // c = [10, "a"]

put()

The put() operator takes two arguments: a hash path and a new element to be inserted. The result is a new data a structure with the element inserted at the location specified by the hash path. If the path specifies a location that does not exist, it will be created. For example:

Code Block
themeConfluence
languagejavascript
a = { "colors" : "many",

...


      "pi" : [3,1,4,1,5,6,9]

...


    };

...


b = {"flop" : 12};

...


c = a.put(["foo"], b);

...


// c = { "colors" : "many",

...


//       "pi" : [3,1,4,1,5,6,9],

...


//       "foo" : {"flop" : 12}

...


//     };

...


d = a.put(["foo", "bar"], b);

...


// d = { "colors" : "many",

...


//       "pi" : [3,1,4,1,5,6,9],

...


//       "foo" : {"bar" : {"flop" : 12}}

...


// };