Home > Mobile >  I wanted to modify the response for array of object
I wanted to modify the response for array of object

Time:10-19

I wanted to modify the response for array of object. I have below result. And i wanted to change the response to specific response.

let result = [
  {
    team_id: 1,
    team_name: 'Avengers',
    participant1: 98,
    participant2: 99,
    participant3: 100,
    participant4: 101,
    phase1: 0,
    phase2: 0,
    phase3: 0,
    phase4: 0,
    phase5: 0,
    participant1_name: 'test 1003',
    participant2_name: 'test 1002',
    participant3_name: 'test 1004',
    participant4_name: 'test 1005'
  }
]

And wanted to convert to below.

[
{
    "team_id": 1,
    "team_name": "Avengers",
    "phase1": 0,
    "phase2": 0,
    "phase3": 0,
    "phase4": 0,
    "phase5": 0,
    "participantDetails": [
        {
            "participant1": 98,
            "participant1_name": "test 1003"
        },
        {
            "participant2": 99,
            "participant2_name": "test 1002"
        },
        {
            "participant3": 100,
            "participant3_name": "test 1004"
        },
        {
            "participant4": 101,
            "participant4_name": "test 1005"
        }
    ]
}

]

I have tried below: -

const data = result.map((elem) => {
                    const participantDetails = [];
                    for (let keys in elem) {
                      if (keys.startsWith('participant')) {
                        console.log('elem:-',elem);
                        participantDetails.push({
                          [keys]: elem[keys]
                        })
                        delete elem[keys]
                      }
                    }
                    return {
                      ...elem,
                      participantDetails: participantDetails
                    }
                  }); 

I have filter participant but not sure how to filter names also. And they are having different positions for that.

Please help thanks in advance.

CodePudding user response:

You're already in the right direction. You don't have to filter for participant names since you can easily derive them once you filtered the participant.

Here's my approach:

let result = [
    {
        team_id: 1,
        team_name: 'Avengers',
        participant1: 98,
        participant2: 99,
        participant3: 100,
        participant4: 101,
        phase1: 0,
        phase2: 0,
        phase3: 0,
        phase4: 0,
        phase5: 0,
        participant1_name: 'test 1003',
        participant2_name: 'test 1002',
        participant3_name: 'test 1004',
        participant4_name: 'test 1005',
    },
];
for (let obj of result) {
    let participantDetails = [];
    Object.keys(obj)
        .filter((key) => /^participant\d $/.test(key))
        .forEach((key) => {
            participantDetails.push({
                [key]: obj[key],
                [`${key}_name`]: obj[`${key}_name`],
            });
            delete obj[key];
            delete obj[`${key}_name`];
        });
    obj.participantDetails = participantDetails;
}

console.log(JSON.stringify(result, null, 4));

Notice that I used regex to filter strictly on participant. From there, I can derive the values for participant_names and push them into the participantDetails array.

It is also important to note that you are modifying the values of the result array. If you want to keep those values, you can use map instead to create a new instance of the array.

  • Related