JSFiddle - React, Tailwind, and code Playground

by David Buzatto

JavaScript

var json = {
  "location": {
    "title1x": {
      "1": "name_1x",
      "2": "name_2x",
      "3": "name_3x",
    },
    "title2y": {
      "1": "name_1y",
      "2": "name_2y",
      "3": "name_3y",
    },
  },
  "object": {
    "title1a": {
      "1": "name_1z",
      "2": "name_2z",
      "3": "name_3z",
    },
    "title2b": {
      "1": "name_1a",
      "2": "name_2a",
      "3": "name_3a",
      "foo": [{
        "x": "aaa",
        "y": "bbb",
        "z": {
          "k": "name_3y"
        }
      }, {
        "x": "aaa",
        "y": "bbb",
        "z": {
          "k": "name_3y",
          "bar": [{
            "op": "test",
            "fooAgain": {
              "x": "name_3y"
            }
          }]
        }
      }]
    },
  }
};

function search(what, where) {

  var results = [];
  var parentStack = [];

  var searchR = function(what, where) {

    if (typeof where == "object") {
      parentStack.push(where);
      for (key in where) {
        searchR(what, where[key]);
      };
      parentStack.pop();
    } else {
      // here comes your search
      if (what === where) {
        results.push({
          parent: parentStack[parentStack.length - 1],
          value: where
        });
      }
    }

  }

  searchR(what, where);

  return results;

}

search("name_3y", json).forEach(function(value, key) {

  var out = "parent: \n";

  for (key in value.parent) {
    out += "    key: " + key + " value: " + value.parent[key] + "\n";
  }

  out += "\nvalue: " + value.value;

  alert(out);

});