Javascript 从依赖于内部变量的json内部获取数据

Javascript 从依赖于内部变量的json内部获取数据,javascript,json,Javascript,Json,假设我有这个随机json文件: { "categoryName": "Appetizers & Sides", "categories": [ { "categoryID": "294", "parentID": "304", "subjectID": "7", "categoryName": "Apps and Side Dishes (Laura)", "categoryDescription": "

假设我有这个随机json文件:

    {
  "categoryName": "Appetizers & Sides",
  "categories": [
    {
      "categoryID": "294",
      "parentID": "304",
      "subjectID": "7",
      "categoryName": "Apps and Side Dishes (Laura)",
      "categoryDescription": "Learn to make amazing appetizers and side dishes with Laura in the Kitchen.",
      "videosCount": "101",
      "forumCategoryID": "163"
    },
    {
      "categoryID": "285",
      "parentID": "304",
      "subjectID": "7",
      "categoryName": "Side Dishes",
      "categoryDescription": "Side dish recipes for salads, vegetables, sauces with Hilah cooking.",
      "videosCount": "38",
      "forumCategoryID": "163"
    }
  ]
}
如果我有categoryID,我怎么能得到categoryName

非常感谢

托马斯使用:


顺便说一句:如果筛选器没有找到结果,它可能会返回一个空集合。

如果我可以问的话,[0]的功能是什么?筛选器返回一个数组,因为您知道parentId是唯一的,所以它将始终返回一个包含单个元素的数组。这就是我使用[0]的原因。
var obj =  {
  "categoryName": "Appetizers & Sides",
  "categories": [
    {
      "categoryID": "294",
      "parentID": "304",
      "subjectID": "7",
      "categoryName": "Apps and Side Dishes (Laura)",
      "categoryDescription": "Learn to make amazing appetizers and side dishes with Laura in the Kitchen.",
      "videosCount": "101",
      "forumCategoryID": "163"
    },
    {
      "categoryID": "285",
      "parentID": "304",
      "subjectID": "7",
      "categoryName": "Side Dishes",
      "categoryDescription": "Side dish recipes for salads, vegetables, sauces with Hilah cooking.",
      "videosCount": "38",
      "forumCategoryID": "163"
    }
  ]
};
var parentId = "304";
var catId = obj.categories.filter(function(item){ return item.parentID === parentId })[0].categoryID;