1

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}] }

1
  • Please elaborate more on what exactly you're trying to achieve, and you'll get better engagement on this question. An expected result is a good start. Commented Apr 20, 2022 at 13:22

1 Answer 1

1

You can map the itemCodes

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

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

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

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.