Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Get a new array from an array of object

I want to create a new array with the ‘itemDetails’ from the following array. In this array inside every ‘poDetails’, ‘itemDetails’ is listed, I need to create a new array with all these ‘itemDetails'(if it is repeated also). I tried some methods using map, foreach, but whiling pushing to new array its not getting as expected

expecting result = 
[ {"cic": 36200097,"description": "TEST ITEM","qty": 312},
  {"cic": 36200038,"description": "TEST ITEM","qty": 156},
  {"cic": 36200097,"description": "TEST ITEM","qty": 468},
  {"cic": 36200038,"description": "TEST ITEM","qty": 156},
  {"cic": 36200097,"description": "TEST ITEM","qty": 468}]

   obj= {
      "toolName": "WHSE Case",
      "poDetails": [
        {
          "po": 678676,
          "itemDetails": [
            {
              "cic": 36200097,
              "description": "TEST ITEM",
              "qty": 312,
            },
            {
              "cic": 36200038,
              "description": "TEST ITEM",
              "qty": 156,
            }
          ]
        },
        {
          "po": 680510,
          "itemDetails": [
            {
              "cic": 36200097,
              "description": "TEST ITEM",
              "qty": 468,
            },
            {
              "cic": 36200038,
              "description": "TEST ITEM",
              "qty": 156,
            }
          ]
        },
        {
          "po": 682110,
          "itemDetails": [
            {
              "cic": 36200097,
              "description": "TEST ITEM",
              "qty": 468,
            }
          ]
        },
      ]
    }

this.gridRows = array.poDetails;
 this.gridRows.forEach(row => {
      if (row.itemDetails) {
        this.formatedData.push(row.itemDetails);
      }
    });

this is waht I tried and not getting the result as expected.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

If you tried already, you clearly didn’t try hard enough. A simple map + flat is all it takes.

const data = {
  toolName: 'WHSE Case',
  poDetails: [
    {
      po: 678676,
      itemDetails: [
        { cic: 36200097, description: 'TEST ITEM', qty: 312, },
        { cic: 36200038, description: 'TEST ITEM', qty: 156, },
      ],
    },
    {
      po: 680510,
      itemDetails: [
        { cic: 36200097, description: 'TEST ITEM', qty: 468, },
        { cic: 36200038, description: 'TEST ITEM', qty: 156, },
      ],
    },
    {
      po: 682110,
      itemDetails: [
        { cic: 36200097, description: 'TEST ITEM', qty: 468, },
      ],
    },
  ],
};


const grouped = data.poDetails.map(({ itemDetails }) => itemDetails).flat(1);

console.log(grouped);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading