skip to Main Content

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


  1. With a combined use of map() and join() operations, and taking advantage of the fact that the default toString() result of an array is already a comma-separated string:

    const result = data.map((g) => [g.name, g.stud.map((s) => s.name)]).join(';');
    

    Complete snippet:

    const data = [{
      "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"
      }]
    }];
    
    const result = data.map((g) => [g.name, g.stud.map((s) => s.name)]).join(';');
    
    console.log(result);
    Login or Signup to reply.
  2. The required output can be achieved using forEach.

    var 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', },], },];
    
    var result = '';
    
    grpdet.forEach((elem, index) => {
      result += elem.name;
      elem.stud.forEach((s, i) => {
        result += ',' + s.name;
      });
      result += ';';
    });
    console.log(result);
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search