Syntax
ST.select(data, selectorFunction)
data
: Any JavaScript objectselectorFunction
: selectorFunction is a predicate, to test each key/value pairs of the entire JSON tree. Return true to keep the element, false otherwise, taking two arguments:key
: the "key" of the current key/value pair being testedvalue
: the "value" of the current key/value pair being testedReturns a selection object which can be queried to retrieve the final result, using the following API:
values()
: Array of values for all the key/value pair matcheskeys()
: Array of keys for all the key/value pair matchespaths()
: Array of paths leading to all the key/value pair matchesobjects()
: Array of full objects in which the key/value match was foundroot()
: Retrieve the root node. Useful when used along with transformYou can select any JSON tree to find only the subtrees that satisfy your needs.
1. Take any data
var data = {
links: [
{ "remote_url": "http://localhost" },
{ "file_url": "file://documents" },
{ "remote_url": "https://blahblah.com" }
],
preview: "https://image",
metadata: "This is a link collection"
};
2. Select subtree
var selection = ST.select(data, function(key, val) {
return /https?:/.test(val);
})
3. Query the selection object
var selected_values = selection.values();
// [
// "http://localhost",
// "https://blahblah.com",
// "https://image"
// ]
var selected_keys = selection.keys();
// [
// "remote_url",
// "remote_url",
// "preview"
// ]
var selected_objects = selection.objects();
// [
// { "remote_url": "http://localhost" },
// { "remote_url": "https://blahblah.com" },
// {
// "links": [
// { "remote_url": "http://localhost" },
// { "file_url": "file://documents" },
// { "remote_url": "https://blahblah.com" }
// ],
// "preview": "https://image",
// "metadata": "This is a link collection"
// }
// ]
//
//
var selected_paths = selection.paths();
// [
// "[\"links\"][0]",
// "[\"links\"][2]",
// ""
// ]