JSFiddle - React, Tailwind, and code Playground
JavaScript
var jsondata={
"item": {
"T1": [
{
"name": "Ice creams",
"T2": [
{
"name": "Ice creams***Stick",
"T3": [
{
"name": "Ice creams***Stick***KoolCool",
"T4": [
{
"name": "Ice creams***Stick***KoolCool***Strawbeerry",
"leaf": [
{
"crust_name": "crust"
}
]
}
]
}
]
}
]
}
]
}
}
function search(name) {
results = [];
cancel = false;
recursiveSearch(name, jsondata);
return results;
}
function recursiveSearch(name, json, startSaving, parentJson) {
if (cancel) return;
if (startSaving) {
if (parentJson["leaf"]) {
results.push("leaf");
results.push(parentJson["leaf"]);
cancel = true; //pushing leaf twice for somereason, work around with 'cancel'
return;
} else if (json["name"]) {
results.push(json["name"]);
return;
} else if (json["leaf"]) {
results.push(json["leaf"]);
cancel = true; //pushing leaf twice for somereason, work around with 'cancel'
return;
}
}
if (isArray(json)) {
for (var i = 0; i < json.length; i++) {
recursiveSearch(name, json[i], startSaving, json);
}
} else {
if (isObject(json)) {
for (key in json) {
if...