'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}] }
Solution 1:[1]
You can map the itemCodes
element.DocumentLines.map(v => v.ItemCode)
This will return the array of ItemCodes from your first array.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
