1

In my NodeJs project i want to create new array form MySQL query result.

This is my result:

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

In this result two data's dayName are same.

This is my expected result

[
    {   
        "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"
            }
        ]
    }
]

My code:

CubbersAvailability.findAll(
    {
        where:{
            cubbersId:1,
            userId:2
        },
        attributes:['availabilityId', 'dayName', 'fromTime', 'toTime']
    }
).then(availabilityList =>{
    res.send(availabilityList);
    // code for create new array
}).catch(error=>{
    res.status(403).send({status: 'error', resCode:200, msg:'Internal Server Error...!', data:error});
});

1 Answer 1

3

Simple enough, but very specific.

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

function createNewArray(source) {

  let output = [];

  source.forEach(entry => {

    let dayName = entry.dayName;
    let time = {
      "availabilityId": entry.availabilityId,
      "fromTime": entry.fromTime,
      "toTime": entry.toTime
    };

    if (output[dayName] === undefined) {
      output[dayName] = {
        "dayName": entry.dayName,
        "time": []
      };
    }

    output[dayName].time.push(time);

  });

  output.shift(); //shouldn't be needed, but since [0] is empty, necessary.

  return output;

}

console.log(createNewArray(source));

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

6 Comments

Thanks for your code it working but result came linke this ibb.co/bHTkw6w
But see my expected result if same dayName means that data in same array
are you there bro
Corrected the structure, but for some reason, it adds a undefined in the beginning, guessing cause there is no 0 in the array, just make a check if the entry is undefined and skip it if it is.
Now it's working bro but top of the result get undefined
|

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.