1

I'm trying to accessing a json child object which is not in an array. i've tried accessing it with my below script but its not working. i want to be able to access the menuCategory Object

JSON

 [
  {
    "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"
    }
  }
]

JAVASCRIPT

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

4 Answers 4

1

May be like

const parent = [{"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"}}]

console.log(parent[0].menuCategory.id);

Sign up to request clarification or add additional context in comments.

Comments

1

If the parent argument in the callEditMenu function is referring to the JSON you included then try parent[0].menuCategory.id

Comments

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

In case you need to find it dynamically. Above are two different ways

Comments

0

Considering there would be multiple items in your array of objects, you can iterate through each object to get the menuCategory name as

let obj = [
  {
    "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"
    }
  }
];

obj.forEach(elem => {
  console.log(elem.menuCategory.name);
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.