0

I want to find single json objec based on ID, from below tree. example - getObjeById(4),

it should return obj from below tree. need help on this.

data={
  "mytree": {
    "id": "dectree",
    "dt": {
      "choice": {
        "id": 0,
        "title": "Which color",
        "description": "Choose color ?",
        "choice": [
          {
            "id": 1,
            "title": "Yellow",
            "description": "Yellow ? ",

            "choice": [
              {
                "id": 5,
                "title": "Dark Yellow",
                "description": "Dark Yellow ,
                "choice": [
                  {
                    "id": 6,
                    "title": "id 6 yello",
                    "description": "<span> last leaf for yello </span>"
                  }]
              },
              {
                "id": 4,
                "title": "Light Yellow",
                "description": "Light Yellow 
              }
            ]
          },
          {
            "id": 2,
            "title": "Red",
            "description": "Red ?"
          },
          {
            "id": 3,
            "title": "Green",
            "description": "Green 
          },
          {
            "id": 7,
            "title": "white",
            "description": "white color",
             "choice": [
                  {
                    "id": 8,
                    "title": "id 8 white",
                    "description": "<span> last leaf for white </span>"
                  }]
          }
        ]
      }
    }
  }
}
brk
  • 48,835
  • 10
  • 56
  • 78
user3215858
  • 29
  • 1
  • 8
  • Recursive code is very expensive. Is there no way you could reorganize your tree? – Emil S. Jørgensen Jul 25 '16 at 13:51
  • Have you tried this solution? - http://stackoverflow.com/questions/10679580/javascript-search-inside-a-json-object – Renzo Gaspary Jul 25 '16 at 13:52
  • I am ok if there is any other way to find object form tree. – user3215858 Jul 26 '16 at 06:38
  • It would be better to remap the object into a way that is optimized for your use case. – epascarello Jul 26 '16 at 16:03
  • Please proofread your title and also question body. –  Jul 26 '16 at 16:10
  • @EmilS.Jørgensen In what sense is recursive code "very expensive"? –  Jul 26 '16 at 16:11
  • @torazaburo recursive code means calling a function with some element, match properties and then calling the function again, this time with some child node. Each call means allocating memory and thus moving closer to a stack overflow. Several restrictions like max call stack applies and many browsers throw `InternalError: too much recursion` if your code looks too recursive, to prevent browser crash. There is also an argument to be made for coding with native methods such as `Array.filter`, since native functionality is almost always faster. – Emil S. Jørgensen Jul 27 '16 at 10:34

2 Answers2

0

Below is a snippet showcasing a recursive search function.

As warned, this function takes approximately 6 milliseconds to search this tree, about a third of a standard 60 fps frame.

var data = {
  "mytree": {
    "id": "dectree",
    "dt": {
      "choice": {
        "id": 0,
        "title": "Which color",
        "description": "Choose color ?",
        "choice": [{
          "id": 1,
          "title": "Yellow",
          "description": "Yellow ? ",
          "choice": [{
            "id": 5,
            "title": "Dark Yellow",
            "description": "Dark Yellow",
            "choice": [{
              "id": 6,
              "title": "id 6 yello",
              "description": "<span> last leaf for yello </span>"
            }]
          }, {
            "id": 4,
            "title": "Light Yellow",
            "description": "Light Yellow"
          }]
        }, {
          "id": 2,
          "title": "Red",
          "description": "Red ?"
        }, {
          "id": 3,
          "title": "Green",
          "description": "Green"
        }, {
          "id": 7,
          "title": "white",
          "description": "white color",
          "choice": [{
            "id": 8,
            "title": "id 8 white",
            "description": "<span> last leaf for white </span>"
          }]
        }]
      }
    }
  }
};
//Here comes the recursive function
function searchTree(data, idLabel, idValue, results) {
  if (idLabel === void 0) {
    idLabel = "id";
  }
  if (idValue === void 0) {
    idValue = "0";
  }
  if (results === void 0) {
    results = [];
  }
  var keys = Object.keys(data);
  keys.forEach(function search(key) {
    if (typeof data[key] == "object") {
      results = searchTree(data[key], idLabel, idValue, results);
    } else {
      if (data[key] == idValue && key == idLabel) {
        results.push(data);
      }
    }
  });
  return results;
}
console.log("Looking for 4:", searchTree(data, "id", "4"));
console.log("Looking for 6:", searchTree(data, "id", "6"));

EDIT - flat structure

An ideal structure would properly look more like this:

var data = [{
  id: 1,
  title: "Yellow",
  description: "Yellow ? ",
  choices: [4, 5]
}, {
  id: 2,
  title: "Red",
  description: "Red ?",
  choices: []
}, {
  id: 3,
  title: "Green",
  description: "Green",
  choices: []
}, {
  id: 4,
  title: "Light Yellow",
  description: "Light Yellow",
  choices: []
}, {
  id: 5,
  title: "Dark Yellow",
  description: "Dark Yellow",
  choices: [6]
}, {
  id: 6,
  title: "id 6 yello",
  description: "<span> last leaf for yello </span>",
  choices: []
}, {
  id: 7,
  title: "white",
  description: "white color",
  choices: [8]
}, {
  id: 8,
  title: "id 8 white",
  description: "<span> last leaf for white </span>",
  choices: []
}];

console.log("Get elements with id == 7", data.filter(function(i) {
  return i.id === 7
})[0]);
console.log("Get elements with id == 2", data.filter(function(i) {
  return i.id === 1
})[0]);
console.log("Get elements with id == 3 or id == 4", data.filter(function(i) {
  return i.id === 3 || i.id === 4
}));

With a structure like above, traversing the tree using filter becomes trivial. Approximately 2 milliseconds calculation time on this structure and it should scale much better.

From here, we could also easily sort our list or manipulate it in a bunch of ways using optimized, native functionality.

Emil S. Jørgensen
  • 6,216
  • 1
  • 15
  • 28
0

is there any way to find immeida parent form node ? I am geeting specific now example id : 5 and it maye be part of one parent whcih is id:3.

user3215858
  • 29
  • 1
  • 8