1

I have below data from my API call and I am working with reduce function to set data as per my requirement. But not able to produce it, please check below code and required output. Also check the function I am trying and suggest me way to create the response.

API Response:

let cmpRecords =  {
    "status": "success",
    "message": "Record Fetched Successfully",
    "data": [
        {
            "_id": "6098ff60a8e9ee2c7c116d6e",
            "record": {
                "creatorName": "ABC",
                "modifierName": ""
            },
            "is_deleted": "0"
        },
        {
            "_id": "6098ff60a8e9ee2c7c116d6e",
            "record": {
                "creatorName": "ABC",
                "modifierName": ""
            },
            "is_deleted": "0"
        },
        {
            "_id": "6098ff60a8e9ee2c7c116d6z",
            "record": {
                "creatorName": "XYZ",
                "modifierName": ""
            },
            "is_deleted": "0"
        },
        ...
    ]
}

Required Output ::

{
    "status": "success",
    "message": "Record Fetched Successfully",
    "data": [
        {
            "name": "ABC",
            "record": [
                {
                    "_id": "6098ff60a8e9ee2c7c116d6e",
                    "record": {
                        "creatorName": "ABC",
                        "modifierName": ""
                    },
                    "is_deleted": "0"
                },
                {
                    "_id": "6098ff60a8e9ee2c7c116d6e",
                    "record": {
                        "creatorName": "ABC",
                        "modifierName": ""
                    },
                    "is_deleted": "0"
                }
            ]
        },
        {
            "name": "XYZ",
            "record": [
                {
                    "_id": "6098ff60a8e9ee2c7c116d6z",
                    "record": {
                        "creatorName": "XYZ",
                        "modifierName": ""
                    },
                    "is_deleted": "0"
                }
            ]
        }
    ]
}

As I want to seperate record of same user, with key and value & key and object I used reduce function for this

group = cmpRecords.reduce((r, a) => {
    let name = a.record.creatorName;
    let record = a;

    r[name] = [...r[name] || [], record];

    return r;
}, {});

This responds with

"data": [
    "ABC":[
        {
            "_id": "6098ff60a8e9ee2c7c116d6e",
            "record": {
                "creatorName": "ABC",
                "modifierName": ""
            },
            "is_deleted": "0"
        },
        ...
    ]
]

I tried other ways but not getting required response.

2
  • 1
    In your output, data should be an array of Objects where each would have both name and record as attributes. Commented Aug 27, 2021 at 7:34
  • @pistou Yes, I actually created response manually so I missed that part. Commented Aug 27, 2021 at 8:24

1 Answer 1

1

You can use Array.reduce() to group your records by name, the output here should be what you require:

        
const cmpRecords =  { "status": "success", "message": "Record Fetched Successfully", "data": [ { "_id": "6098ff60a8e9ee2c7c116d6e", "record": { "creatorName": "ABC", "modifierName": "" }, "is_deleted": "0" }, { "_id": "6098ff60a8e9ee2c7c116d6e", "record": { "creatorName": "ABC", "modifierName": "" }, "is_deleted": "0" }, { "_id": "6098ff60a8e9ee2c7c116d6z", "record": { "creatorName": "XYZ", "modifierName": "" }, "is_deleted": "0" }, ] }

const output = {
    ...cmpRecords,
    data: Object.values(cmpRecords.data.reduce((acc, cur) => { 
        acc[cur.record.creatorName] = acc[cur.record.creatorName] || { name: cur.record.creatorName, record: [] };
        acc[cur.record.creatorName].record.push(cur);
        return acc;
    }, {}))
}

console.log('Output:', output)
 

Sign up to request clarification or add additional context in comments.

1 Comment

Glad to be of help!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.