const data = [
{
"id": 1,
"value": 1,
"label": "333",
"children": [
{
"value": 1,
"shipping_id": 1,
"label": "2313"
},
{
"value": 2,
"shipping_id": 1,
"label": "546464567546"
},
{
"value": 3,
"shipping_id": 1,
"label": "331"
}
]
},
{
"id": 2,
"value": 2,
"label": "354534",
"children": []
}
];
function transformData(data, callback) {
return data.map(item => {
const transformedItem = {
text: item.label,
value: item.value
};
if (item.children && item.children.length > 0) {
transformedItem.children = transformData(item.children, callback);
}
return callback(transformedItem);
});
}
const result = transformData(data, item => item);
console.log(result);