0

I have this JSON response where I want to push "status": "pending", inside the nested Menu array, Please help how I can achieve this in Javascript.

[
{
    "id": 1,
    "status": "pending",
    "menues": [
        {
            "title": "Coke",
            "price": "200"
        }
    ]
},
{
    "id": 2,
    "status": "delivered",
    "menues": [
        {
            "title": "Pepsi",
            "price": "120"
        }
    ]
}
]

Here is what I want to achieve: I just want to push the Staus key/value inside the Menu array

[
{
    "id": 1,
    "menues": [
        {
            "title": "Coke",
            "price": "200",
            "status": "pending",
        }
    ]
},
{
    "id": 2,
    "menues": [
        {
            "title": "Pepsi",
            "price": "120",
            "status": "delivered",
        }
    ]
}
] 
1
  • 1
    What methods have you tried? Commented Aug 28, 2021 at 21:54

2 Answers 2

1

You can go over the array, and for each item, go over the items in menues. Using the forEach method, this can even be done as a single statement:

arr = [
  {
      "id": 1,
      "status": "pending",
      "menues": [
          {
              "title": "Coke",
              "price": "200"
          }
      ]
  },
  {
      "id": 2,
      "status": "delivered",
      "menues": [
          {
              "title": "Pepsi",
              "price": "120"
          }
      ]
  }
];

arr.forEach(nested => {
    nested.menues.forEach(menu => menu.status = nested.status);
    delete nested.status
});
console.log(arr);

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

7 Comments

What about if there are multiple statuses like he has another "status": "delivered" ?So always have to run multiple loops?
@hamzaliaqat no - just put the logic in inner forEach to decide what status to assign.
@Mureinik I think the OP has the desired status in the object itself, we just want to push it into the array while going through your loop
@kevmc oh, indeed! I completely missed the brief there. Edited and fixed.
@hamzaliaqat I misread the question. Thanks to kevmc pointing this out, I've edited my answer to provide the required functionality.
|
0

Maybe this is what you want? The following script creates a new nested array structure, leaving the original unchanged. Instead of deleting the status property from the outer object I limit the creation of properties in the newly created object to id and menues.

I changed this answer in response to OP's comment asking for ES5 and ES6 methods and the ... operator.

arr = [
  {
      "id": 1,
      "status": "pending",
      "menues": [
          {
              "title": "Coke",
              "price": "200"
          }
      ]
  },
  {
      "id": 2,
      "status": "delivered",
      "menues": [
          {
              "title": "Pepsi",
              "price": "120"
          }
      ]
  }
];

const res=arr.map(nested =>({ id:nested.id, menues:
  nested.menues.map(menu =>({...menu,status:nested.status})) }));
console.log(res);

3 Comments

Is it possible with ES5 or ES6? Like with spread operator and map method?
Please check again, I changed my answer.
thank you so much, there was an issue when I was applying the first solution with forEach loop but now it's worked perfect and saved my day, once again thank you for your valuable answer

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.