I want to loop through req.body payload object in a nodejs application and get the array object with SplitType = 'FLAT'. So after I finish looping through the array object I'll push the value into an empty array. I'm getting an error saying the SplitType is not defined. below is the object array that I want to loop through.
{
"ID": 13092,
"Amount": 4500,
"Currency": "NGN",
"CustomerEmail": "anon8@customers.io",
"SplitInfo": [
{
"SplitType": "FLAT",
"SplitValue": 450,
"SplitEntityId": "LNPYACC0019"
},
{
"SplitType": "RATIO",
"SplitValue": 3,
"SplitEntityId": "LNPYACC0011"
},
{
"SplitType": "PERCENTAGE",
"SplitValue": 3,
"SplitEntityId": "LNPYACC0015"
},
{
"SplitType": "RATIO",
"SplitValue": 2,
"SplitEntityId": "LNPYACC0016"
},
{
"SplitType": "FLAT",
"SplitValue": 2450,
"SplitEntityId": "LNPYACC0029"
},
{
"SplitType": "PERCENTAGE",
"SplitValue": 10,
"SplitEntityId": "LNPYACC0215"
},
]
}
Here is the for loop code
for (let i = 0; i = splitInfo.length; i++) {
if(splitInfo[i].SplitType === 'FLAT') {
result.initialBalance = flat(initialBalance, splitInfo.SplitValue);
result.SplitBreakdown.push({
SplitEntityId: splitInfo.SplitEntityId,
Amount: splitInfo.SplitValue
});
}
}
Your loop is wrong, I think you mean:
for (let i = 0; i < splitInfo.length; i++) {