I have this object:
const plans = tests: {
[TestID: string]: {
test_id: number;
test_name: string;
cases: {
[caseID: string]: {
case_id: number;
case_name: string;
};
};
};
}
I converted the object to an array ( const plansArray = Object.values(plans);) so that I can map it in my react code. I want to know if there is a way to optimize or find a better way to do what I am doing below:
{plansArray &&
plansArray.map((Plan) => (
<div key={Plan.test_id}>
//The NestedDropdown is just an accordion - the title prop will display on the accordion
<NestedDropdown
title={Plan.test_name}
>
// This is my concern, is there a better way to handle this?
{Object.values(Plan.cases).map((cases) => (
<NestedDropdown
key={cases.case_id}
title={`- ${cases.case_name}`}
></NestedDropdown>
))}
</NestedDropdown>
</div>
))}
As you can see I am mapping over the Plan to access the nested cases object. I there a way to only map once?
yudhieshsays, but isn't this a case of premature optimization? I wouldn't say nested loops are bad per se. It depends on the situation whether you should use them. If you're not experiencing problems with the approach you could also just keep it this way.