searchObj
by Hans PUFAL
JavaScript
TestObj = {
"Categories": [{
"Products": [{
"id": "a01",
"name": "Pine",
"description": "Short description of pine."
},
{
"id": "a02",
"name": "Pine",
"description": "Short description of pine."
},
{
"id": "a03",
"name": "Poplar",
"description": "Short description of poplar."
}],
"id": "A",
"title": "Cheap",
"description": "Short description of category A."
},
{
"Product": [{
"id": "b01",
"name": "Maple",
"description": "Short description of maple."
},
{
"id": "b02",
"name": "Oak",
"description": "Short description of oak."
},
{
"id": "b03",
"name": "Bamboo",
"description": "Short description of bamboo."
}],
"id": "B",
"title": "Moderate",
"description": "Short description of category B."
}]
};
function searchObj (obj, search, key, newval) {
/* obj : object to search
search : search criteria
key : key to update on match (optional)
newval : new value if key specified
Search obj recursively looking for a component which matches
the search criteria. If found and key/newval are specified then
update the found component accordingly.
search may be a string or an array.
If a string, it is a comma separated list of key names
optionally followed by : or = and a value.
If an array each element must be a key string or a array
of two elements specifying a key string and a value.
Search proceeds as follows. For each object component of obj, check if
all elements of the search match. A match checks for presence of the
key and, if a value is specified that that key equals the value.
If...