Below is a sample array which has a multidimensional array. I need to separate the values as comma separated and semicolon separated as the expected output
"grpdet": [
{
"id": 36,
"name": "GROUP TOP A",
"stud": [
{
"id": 114,
"name": "test_stu_1"
},
{
"id": 115,
"name": "test_stu_2"
}
]
},
{
"id": 37,
"name": "GROUP TOP B",
"stud": [
{
"id": 116,
"name": "test_stu_3"
},
{
"id": 117,
"name": "test_stu_4"
}
]
},
{
"id": 38,
"name": "GROUP TOP C",
"stud": [
{
"id": 118,
"name": "test_stu_5"
},
{
"id": 119,
"name": "test_stu_6"
}
]
},
{
"id": 39,
"name": "GROUP TOP D",
"stud": [
{
"id": 120,
"name": "test_stu_7"
},
{
"id": 121,
"name": "test_stu_8"
}
]
}
]
Expected output:
GROUP TOP A,test_stu_1,test_stu_2;GROUP TOP B,test_stu_3,test_stu_4;GROUP TOP C,test_stu_4,test_stu_5;
I have tried using forEach
but ;
and ,
appear in the same line.
2
Answers
With a combined use of
map()
andjoin()
operations, and taking advantage of the fact that the defaulttoString()
result of an array is already a comma-separated string:Complete snippet:
The required output can be achieved using forEach.