Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/33.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
Javascript 访问不在数组中的json子对象_Javascript_Angular - Fatal编程技术网

Javascript 访问不在数组中的json子对象

Javascript 访问不在数组中的json子对象,javascript,angular,Javascript,Angular,我试图访问一个不在数组中的json子对象。我试着用下面的脚本访问它,但它不起作用。我希望能够访问menuCategory对象 JSON JAVASCRIPT callEditMenu(parent, content) { this.modalService.open(content); this.editMenuCategoryId = parent.menuCategory.id; } 可能是 const parent=[{“id”:67,“name”:“星期三菜单”,“serve

我试图访问一个不在数组中的json子对象。我试着用下面的脚本访问它,但它不起作用。我希望能够访问menuCategory对象

JSON

JAVASCRIPT

callEditMenu(parent, content) {
  this.modalService.open(content);
  this.editMenuCategoryId = parent.menuCategory.id;
}
可能是

const parent=[{“id”:67,“name”:“星期三菜单”,“serveDate”:“2019-06-12 00:00:00”,“expiryDate”:“2019-06-12 16:11:00”,“status”:“APPROVED”,“isEnabled”:true,“feeds”:[{“id”:45,“name”:“Waakye,Gari和Wele”,“description”:“为所有孩子设计的非常好的食物”,“图像”:“mealType”:“午餐”,“单价”:30,“status”:“ENABLED”},{“id”:46,“name”:“Gari and Beans”,“description:“为所有孩子设计的非常好的食物”,“image:”“mealType:“午餐”,“单价”:12,“status:“ENABLED”}],“menuCategory:{“id”:2,“name:“hello”}]

console.log(parent[0].menuCategory.id);
如果callEditMenu函数中的parent参数引用的是您包含的JSON,请尝试
parent[0].menuCategory.id

let arr = [{"id":67,"name":"Wednesday Menu","serveDate":"2019-06-12 00:00:00","expiryDate":"2019-06-12 16:11:00","status":"APPROVED","isEnabled":true,"meals":[{"id":45,"name":"Waakye, Gari and Wele","description":"A very well designed food for all kids","image":"","mealType":"LUNCH","unitPrice":30,"status":"ENABLED"},{"id":46,"name":"Gari and Beans","description":"A very well designed food for all kidsss","image":"","mealType":"LUNCH","unitPrice":12,"status":"ENABLED"}],"menuCategory":{"id":2,"name":"hello"}}]

for (let item of arr)  {
  if (item.hasOwnProperty("menuCategory")) {
    console.log(item["menuCategory"]);
  }
};

let res = arr.filter((item) => item && item.menuCategory);
console.log(res[0].menuCategory);

如果您需要动态查找它。以上是两种不同的方法

考虑到对象数组中会有多个项,您可以遍历每个对象以获得菜单类别名称,如下所示

让obj=[
{
“id”:67,
“姓名”:“星期三菜单”,
“服务”:“2019-06-12 00:00:00”,
“到期日”:“2019-06-12 16:11:00”,
“状态”:“已批准”,
“isEnabled”:没错,
“膳食”:[
{
“id”:45,
“名称”:“Waakye、Gari和Wele”,
“描述”:“为所有孩子设计的非常好的食物”,
“图像”:“,
“mealType”:“午餐”,
“单价”:30,
“状态”:“已启用”
},
{
“id”:46,
“名称”:“Gari and Beans”,
“描述”:“为所有孩子设计的非常好的食物”,
“图像”:“,
“mealType”:“午餐”,
“单价”:12,
“状态”:“已启用”
}
],
“菜单分类”:{
“id”:2,
“姓名”:“你好”
}
}
];
对象forEach(elem=>{
log(elem.menuCategory.name);
});
let arr = [{"id":67,"name":"Wednesday Menu","serveDate":"2019-06-12 00:00:00","expiryDate":"2019-06-12 16:11:00","status":"APPROVED","isEnabled":true,"meals":[{"id":45,"name":"Waakye, Gari and Wele","description":"A very well designed food for all kids","image":"","mealType":"LUNCH","unitPrice":30,"status":"ENABLED"},{"id":46,"name":"Gari and Beans","description":"A very well designed food for all kidsss","image":"","mealType":"LUNCH","unitPrice":12,"status":"ENABLED"}],"menuCategory":{"id":2,"name":"hello"}}]

for (let item of arr)  {
  if (item.hasOwnProperty("menuCategory")) {
    console.log(item["menuCategory"]);
  }
};

let res = arr.filter((item) => item && item.menuCategory);
console.log(res[0].menuCategory);