Click here to Skip to main content
15,887,267 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I have an object which contains different arrays like
Fruits: Array(4),
Trees:Array(3),
Vegetables:Array(2),…etc.

I want to push all these arrays into one array and display them in separate accordions.

Before Combine Array:
JSON
{
    Fruits: [{label:’Apple’, text:’Red Color’},{label:’Mango’, text:’Yellow Color’},{label:’Orange’, text:’Orange Color’},{label:’Grap’, text:’Black Color’}]
    Trees: [{label:’Maple Tree’, text:’Orange Leafs’},{label:’Redbud Tree’, text:’Pink Leafs’},{label:’Pine’, text:Green Leafs’}]
    Vegetables: [{label:’Tomato’, text:’Red Color’},{label:’PumpKin’, text:’Yellow Color’},{label:’Carrot’, text:’Orange Color’},{label:’Onion’, text:’Red Color’}]
}
I want to combine above arrays into one single array as below by using Typescript and display them in accordion. [
{label:Fruits, values:[ {label:’Apple’, text:’Red Color’},{label:’Mango’, text:’Yellow Color’}, {label:’Orange’, text:’Orange Color’},{label:’Grap’, text:’Black Color’}]},

{label:Trees, values:[{label:’Maple Tree’, text:’Orange Leafs’},{label:’Redbud Tree’, text:’Pink Leafs’},{label:’Pine’, text:Green Leafs’}]},

{label:Vegetables, values:[{label:’Tomato’, text:’Red Color’},{label:’PumpKin’, text:’Yellow Color’},{label:’Carrot’, text:’Orange Color’},{label:’Onion’, text:’Red Color’}]}


What I have tried:

I tried to add arrays by using foreach but no use. So i posted this. Any help regarding this.
Posted
Updated 17-Jul-23 3:39am
v2

1 solution

You haven't explained what you have tried, nor where you are stuck. Therefore, we can only suggest the obvious:
JavaScript
const combined = [
    { label: "Fruits", values: Fruits },
    { label: "Trees", values: Trees },
    { label: "Vegetables", values: Vegetables }
];
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900