Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

Javascript push a key value pair into a nested object array

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

Javascript How to push a key value pair into a nested object array

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

PREVIOUS NEXT
Code Example
Javascript :: angular window object 
Javascript :: responsive grid using antd 
Javascript :: get pods running on a node 
Javascript :: how to get custom attribute value in react 
Javascript :: for in object javascript 
Javascript :: Convert a string to an integer in jQuery 
Javascript :: clear textbox js 
Javascript :: dayjs timezone 
Javascript :: add variable inside regex in javascript 
Javascript :: the submitted data was not a file. check the encoding type on the form django react 
Javascript :: form data append jquery 
Javascript :: javascript send post 
Javascript :: javascript clear interval 
Javascript :: moment get week 
Javascript :: loop through map in js 
Javascript :: shadow in react native 
Javascript :: html javascript find data attribute 
Javascript :: set drain docker node 
Javascript :: text to 64base javascript 
Javascript :: vue.js cdn script 
Javascript :: javascript sort by big amount to small desc 
Javascript :: java password regex 
Javascript :: array contains multiple js 
Javascript :: mongodb add admin user 
Javascript :: update angular materia; 
Javascript :: async storage get item 
Javascript :: js send get method 
Javascript :: convert object to array javascript 
Javascript :: discord js fetch user 
Javascript :: javascript check if json file is empty 
ADD CONTENT
Topic
Content
Source link
Name
1+2 =