1

I have array result, if array values is same create new array list

This is my example array:

[
    {
        "dayName": 1,
        "time": {
            "availabilityId": 1,
            "fromTime": "05:30:00",
            "toTime": "10:00:00"
        }
    },
    {
        "dayName": 1,
        "time": {
            "availabilityId": 2,
            "fromTime": "10:30:00",
            "toTime": "06:00:00"
        }
    },
    {
        "dayName": 2,
        "time": {
            "availabilityId": 3,
            "fromTime": "16:30:00",
            "toTime": "22:00:00"
        }
    }
]

But i want this array like this

[
    {   
        "dayName": 1,
        "time":[
            {
                "availabilityId": 1,
                "fromTime": "05:30:00",
                "toTime": "10:00:00"
            },
            {
                "availabilityId": 2,
                "fromTime": "10:30:00",
                "toTime": "06:00:00"
            }
        ]
    },
    {   
        "dayName": 2,
        "time":[
            {
                "availabilityId": 3,
                "fromTime": "16:30:00",
                "toTime": "22:00:00"
            }
        ]
    }
]

Because in first 2 array dayName are same, so i want to create like array of array

This is code for i tried

CubbersAvailability.findAll(
{
    where:{
            cubbersId:cubbersId,
            userId:userId,
            status:1
        },
        attributes:['availabilityId', 'dayName', 'fromTime', 'toTime']
    }
).then(availabilityList =>{

    let list = [];
    availabilityList.forEach(result => {
       list.push({
            "dayName": result.dayName,
            "time": {
                "availabilityId": result.availabilityId,
                "fromTime": result.fromTime,
                "toTime": result.toTime
            }
        });
});

2 Answers 2

3

You can use reduce() to summarize the array into an object. Use Object.values to convert the object into an array.

let availabilityList = [{"dayName":1,"time":{"availabilityId":1,"fromTime":"05:30:00","toTime":"10:00:00"}},{"dayName":1,"time":{"availabilityId":2,"fromTime":"10:30:00","toTime":"06:00:00"}},{"dayName":2,"time":{"availabilityId":3,"fromTime":"16:30:00","toTime":"22:00:00"}}]

let result = Object.values(availabilityList.reduce((c, {dayName,time}) => {
  c[dayName] = c[dayName] || {dayName,time: []};
  c[dayName].time.push(time);
  return c;
}, {}));

console.log(result);

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

1 Comment

I updated my code with your logic please check that... ;)
1
CubbersAvailability.findAll(
        {
            where:{
                cubbersId:cubbersId,
                userId:userId,
                status:1
            },
            attributes:['availabilityId', 'dayName', 'fromTime', 'toTime']
        }
    ).then(availabilityList =>{

        let list = [];
        availabilityList.forEach(result => {
            list.push({
                "dayName": result.dayName,
                "time": {
                    "availabilityId": result.availabilityId,
                    "fromTime": result.fromTime,
                    "toTime": result.toTime
                }
            });
        });

        let result = Object.values(list.reduce((c, {dayName,time}) => {
        c[dayName] = c[dayName] || {dayName,time: []};
        c[dayName].time.push(time);
        return c;
        }, {}));

        res.status(200).send({status: 'success', resCode:200, data:result});
    }).catch(error=>{
        res.status(403).send({status: 'error', resCode:200, msg:'Internal Server Error...!', data:error});
    });

Result

{
    "status": "success",
    "resCode": 200,
    "data": [
        {
            "dayName": 1,
            "time": [
                {
                    "availabilityId": 1,
                    "fromTime": "05:30:00",
                    "toTime": "10:00:00"
                },
                {
                    "availabilityId": 2,
                    "fromTime": "10:30:00",
                    "toTime": "06:00:00"
                }
            ]
        },
        {
            "dayName": 2,
            "time": [
                {
                    "availabilityId": 3,
                    "fromTime": "16:30:00",
                    "toTime": "22:00:00"
                }
            ]
        }
    ]
}

Comments

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.