0

I just started trying Javascript and I'm struggling. My end result is supposed to be a chronological timeline of activities (Calls, Meetings, Tasks).

I'm receiving a file from an application with different types of records. It contains Calls, Meetings, and Tasks. When I receive the file, they are in no particular order and each record type has different fields. I need to get them into the same table but sorted by date. Here is a sample file with a Call and a Task but it might have 10 or more records of differing type.

[
{
    "Owner": {
        "name": "Raymond Carlson",
    },
    "Check_In_State": null,
    "Activity_Type": "Calls",
    "Call_Start_Time": "2022-10-23T20:00:00-05:00",
    "$editable": true,
    "Call_Agenda": "Need to call and discuss some upcoming events",
    "Subject": "Call scheduled with Florence"
},
{
    "Owner": {
        "name": "Raymond Carlson",
    },
    "Check_In_State": null,
    "Activity_Type": "Tasks",
    "Due_Date": "2022-10-24",
    "$editable": true,
    "Description": "-Review Action Items from Last week”,
    "Subject": "Complete Onboarding"
}
]

This is what I'm doing now and I know it's not the best way to go about it.

         for (var i = 0; i < obj.length; i++) {
 
            var activityType0 = (obj[0].Activity_Type)
            var activityOwner0 = (obj[0].Owner.name);

            if (activityType0 == "Events") {
                start0 = (obj[0].Start_DateTime)
                startDate0 = new Date(start0);
                activityDate0 = startDate0.toLocaleString();
                activityTitle0 = (obj[0].Subject);
                activityDesc0 = (obj[0].Description);
            }
            else if (activityType0 == "Tasks"){
                dueDate0 = (obj[0].Due_Date)
                activityDate0 = dueDate0;
                activityTitle0 = (obj[0].Subject);
                activityDesc0 = (obj[0].Description);
            }
            else if (activityType0 == "Calls"){
                callStart0 = (obj[0].Call_Start_Time)
                callStartTime0 = new Date(callStart0);
                activityDate0 = callStartTime0.toLocaleString();
                activityTitle0 = (obj[0].Subject);
                activityDesc0 = (obj[0].Call_Agenda);
            }
    }

So regardless of the type of record, I have an activityOwner, activityDate, activityTitle, activityDesc, And that's what I need.

Aside from that code above needing work, now my question is, what do I need to do with these values for each record to put them in order by "activityDate". Do I need to put them back into an array then sort and if so, what's the best approach?

Thank you much!

  • 1
    If you just want to sort by date, you can do something like `const getTimestamp = activity => new Date(activity.Start_DateTime ?? activity.Due_Date ?? activity.Call_Start_Time).valueOf()` and then `obj.sort((a, b) => getTimestamp(a) - getTimestamp(b))`. Or do you also want to normalize the data? – CherryDT Oct 24 '22 at 04:43
  • Use the map function to prepare the array of objects where the object's property will be what you want. Also here you can convert the date into a common format. Then use the sort function. You can get some idea regarding the sort function in this link. https://www.freecodecamp.org/news/javascript-array-sort-tutorial-how-to-use-js-sort-methods-with-code-examples/ – Zafor Oct 24 '22 at 05:33

1 Answers1

0

Right now I'm not really sure what your end goal is, is it sorting by activity type or activity date ?

If it's the latter, you can try referring to this answer, or try to sort activity type by the ASCII number of each starting letter in each type (e.g. "C" in "Call", "T" in "Tasks", etc.)

Dennis Quan
  • 149
  • 1
  • 5
  • Thanks Dennis. I think I put "by date" in the question somewhere (I'll edit and put "activityDate"). So based on your link, it sounds like I need to put them back into an array? I don't think there is a way to sort them from the original file since the dates are in different formats. – Nathan Zoho Oct 24 '22 at 04:46