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

ForEach inside Foreach Javascript

Hi how do I loop trough an array i already looped trough? So i foreach the subarray and still having the the old array

array =  [
{
"DocNum": 210446, 
"DocType": "Test",
"DocumentLines": [
{
"LineNum": 0,
"ItemCode": "427"
}, 
{
"LineNum": 0,
"ItemCode": "34"
}
]
},
{
"DocNum": 210446, 
"DocType": "Test",
 "DocumentLines": [
{
  "LineNum": 0,
  "ItemCode": "427"
  }, 
  {
    "LineNum": 0,
    "ItemCode": "34"
    }
  ]
  }
 ]

for (let element of array) {
 newArray.push({
    key1: element.DocNum,
    key2: element.DocType,
    key3: element.DocumentLines[???].ItemCode
})}

So the array will look like this in the end

{ "key1":210446, "key2":Test, "key3": [{ItemCode: 34},{"ItemCode": 427}] }

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 :

You can map the itemCodes

element.DocumentLines.map(v => v.ItemCode)

This will return the array of ItemCodes from your first array.

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