admin管理员组

文章数量:1287201

I want to search a JSON object to check if it contains string values stored in an array. and figure out the parent elements.

var searchVal = ['name_1a','name_2y','name_3x'];

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",
        },
    }
};

I want to pass the results into a function. And deal with them separate.

name_1a -> function(title2b, object)
name_2y -> function(title2y, object)
name_3x -> function(title1x, location) etc.

. This is what I have tried so far. I can't seem to figure out how to gothrough the entire JSON object

var searchVal = ['name_1a','name_2y','name_3x'];
  for (var i=0 ; i < searchVal.length ; i++){
    for (var k=0 ; k < ????.length ; k++)
    {
      if (json.???????? == searchVal[i]) {
        results.push(???????);
        console.log(results);

      }
    }
  }

I want to search a JSON object to check if it contains string values stored in an array. and figure out the parent elements.

var searchVal = ['name_1a','name_2y','name_3x'];

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",
        },
    }
};

I want to pass the results into a function. And deal with them separate.

name_1a -> function(title2b, object)
name_2y -> function(title2y, object)
name_3x -> function(title1x, location) etc.

. This is what I have tried so far. I can't seem to figure out how to gothrough the entire JSON object

var searchVal = ['name_1a','name_2y','name_3x'];
  for (var i=0 ; i < searchVal.length ; i++){
    for (var k=0 ; k < ????.length ; k++)
    {
      if (json.???????? == searchVal[i]) {
        results.push(???????);
        console.log(results);

      }
    }
  }
Share Improve this question edited Dec 13, 2016 at 17:08 D3181 2,1025 gold badges22 silver badges47 bronze badges asked Dec 13, 2016 at 16:15 user2557850user2557850 991 gold badge3 silver badges15 bronze badges 8
  • 1 json is not proper syntax – Mahi Commented Dec 13, 2016 at 16:18
  • You need to make a recursive function – Eric Commented Dec 13, 2016 at 16:20
  • could you give us the expected output corresponding to the input you gave – kevin ternet Commented Dec 13, 2016 at 16:23
  • Are the searched strings allways at the third level or they can be deeper – kevin ternet Commented Dec 13, 2016 at 16:25
  • @kevinternet third level. Results should passed to a function – user2557850 Commented Dec 13, 2016 at 16:40
 |  Show 3 more ments

3 Answers 3

Reset to default 5

with the code below you can find what you are looking for recursively:

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",
        },
    }
};

var searchTest = function(varToSearch, jsonData) {

    for (var key in jsonData) {
        if(typeof(jsonData[key]) === 'object') {
            searchTest(varToSearch, jsonData[key]);
        } else {
            if(jsonData[key] == varToSearch) {
                console.log(jsonData[key]);
            }
        }
    }

}

searchTest('name_1a', json);

Reference:

get data from dynamic key value in json

get value from json with dynamic key

https://trinitytuts./tips/get-dynamic-keys-from-json-data/

How do I enumerate the properties of a JavaScript object?

Check if a value is an object in JavaScript

What about something like this:

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 es 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);

});

The search function will search for a value that is exactly equal inside a json object. You can use it to search for each element of an array for example, just adjust the code. A stack was necessary, since we need to keep track of the parents. I modified your json to insert more levels. The values of the results are objects with two attributes. I think that with this you can do what you need. You can, of course, modify my code to use regular expressions intead of strings in your search. It would be more powerfull.

var searchVal = ['name_1a','name_2y','name_3x'];

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",
        }
    }
};
var getTitle=function(json,val){
  for (var key in json) {
    var titles= json[key];
    for (var tit in titles) {
      var names=titles[tit];
      for (var name in names) {
        var string=names[name];
        if(string===val)
          return tit;
      }
    }
}
}

searchVal.forEach(function(valToSearch){
   console.log(getTitle(json,valToSearch));
});

本文标签: javascriptHow do I search a JSON object for a specific string valueStack Overflow