Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays 如何使用键从json中提取对象?_Arrays_Json_Reactjs_Typescript_Typescript2.0 - Fatal编程技术网

Arrays 如何使用键从json中提取对象?

Arrays 如何使用键从json中提取对象?,arrays,json,reactjs,typescript,typescript2.0,Arrays,Json,Reactjs,Typescript,Typescript2.0,我正在使用打字脚本。我想迭代下面的json文件,并根据搜索字符串提取各个对象 { "menus": { "indian": [ { "id": "000123", "name": "Dish 1" }, { "id": "000124", "name": "Dish 2" }, { "i

我正在使用打字脚本。我想迭代下面的json文件,并根据搜索字符串提取各个对象

{
  "menus": {
    "indian": [
        {
            "id": "000123",
            "name": "Dish 1"
        },
        {
            "id": "000124",
            "name": "Dish 2"
        },
        {
            "id": "000125,
            "name": "Dish 3"
        }
    ],
    "chinese": [
        {
            "id": "000126",
            "name": "Dish 4"
        },
        {
            "id": "000127",
            "name": "Dish 5"
        },
        {
            "id": "000128",
            "name": "Dish 6"
        }
    ],
    "tandoori": [
        {
            "id": "000129",
            "name": "Dish 7"
        },
        {
            "id": "000130",
            "name": "Dish 8"
        },
        {
            "id": "000131",
            "name": "Dish 9"
        }
    ]
   }
 }
我将用我的模型类映射响应json

export interface Menu {
    indian: Item[];
    chinese: Item[];
    tabdoori: Item[];
}       

export interface Item {
    id: string;
    name: string;
}

const response: Menu = this.myApiService.getAllMenus();
例如,我的输入是“indian”,然后我需要从json中提取对象

"indian": [
            {
                "id": "000123",
                "name": "Dish 1"
            },
            {
                "id": "000124",
                "name": "Dish 2"
            },
            {
                "id": "000125,
                "name": "Dish 3"
            }
        ]

如何获取此信息?

这是一个显示相同但来自本地数据的示例。它确实取决于
getAllMenus
返回的内容;我猜它是异步的,所以我们需要知道您在那里使用的是什么。

在TypeScript中,属性和JSON键是不同的<代码>getAllMenus()。在此上下文中,菜单将为您提供一个错误。相反,请使用
getAllMenus()['menus']
notation

export interface Menu {
    indian: Item[];
    chinese: Item[];
    tabdoori: Item[];
}       

export interface Item {
    id: string;
    name: string;
}

function getMenu(response ,name){
   return response['menus'][name]
}
同步访问

如果
getAllMenus
返回一个对象(而不是可观察对象):

异步访问

如果
getAllMenus
返回一个可观察的,并且需要订阅:

this.myApiService.getAllMenus().pipe(
  map( response : Menu => getMenu(response, name))
)
.subscribe( nameItems=> console.log(nameItems) );
输出

[
  {
    "id": "000123",
    "name": "Dish 1"
  },
  {
    "id": "000124",
    "name": "Dish 2"
  },
  {
    "id": "000125,
    "name": "Dish 3"
  }
]

您是否尝试过
getAllMenus().menus.indian
?“indian”是对象中属性的名称。正如科林所说,你可以简单地引用这个属性。但是,如果“indian”是来自某个搜索字段的变量中的值(例如,如果您有这样的代码,
searchTerm=“indian”
),则您可能必须编写…
。菜单[searchTerm]
,以便您可以使用字符串变量访问对象属性。一旦获得该值,就可以简单地循环数组中的项。
[
  {
    "id": "000123",
    "name": "Dish 1"
  },
  {
    "id": "000124",
    "name": "Dish 2"
  },
  {
    "id": "000125,
    "name": "Dish 3"
  }
]