I am trying to iterate through a nested for loop. If a value if equals to something, I want to add that object to an array in my state.
this.state = {
custBehavior: [],
custService: [],
custEngagement: [],
custUsage: []
}
Code
this.props.insights.map((behavior) =>
behavior.map((items) =>
{if (items.scoreCategory === "Behavior") {
this.setState(behaviorInsight => ({
custBehavior: [...behaviorInsight.custBehavior, items]
}))
}}
)
The prop information is the full JSON below.
If I remove the if statement and do this....
this.props.insights.map((behavior) =>
behavior.map((items) =>
console.log(items)
)
);
I'll get a print out of each object within the array; again, sample JSON is below.
[{
"scoreModelId": "DNA_APPLE_HML_TAG",
"scoreDisplayName": "Apple Enthusiast",
"scoreCategory": "Behavior",
"scoreSystem": "LMH",
"scoreValue": "HIGH",
"scoreDate": "2019-01-05",
"scoreLevel": "L",
"scorePriority": 1
}, {
"scoreModelId": "DNA_GOOGLE_HML_TAG",
"scoreDisplayName": "Google Enthusiast",
"scoreCategory": "Behavior",
"scoreSystem": "LMH",
"scoreValue": "HIGH",
"scoreDate": "2019-01-05",
"scoreLevel": "L",
"scorePriority": 1
}, {
"scoreModelId": "MG6M_IN_A",
"scoreDisplayName": "LTV Model",
"scoreCategory": "Segmentation",
"scoreSystem": "DECIMAL",
"scoreValue": "14.06",
"scoreDecile": "2",
"scoreCentile": "13",
"scoreDate": "2019-01-14",
"scoreLevel": "L"
}]
I can't see what I am missing here.