0

I have 2 objects and I want to merge it as one object array but I need first to compare using JavaScript or AngularJS.

A = [
     {date: "2013-07-31", start_time:"2013-07-31 17:30:00+10", finish_time:"2013-07-31 20:30:00+10"},
     {date: "2013-08-03", start_time:"2013-08-03 17:00:00+10", finish_time:"2013-08-03 20:00:00+10"},
     {date: "2013-09-03", start_time:"2013-09-03 17:00:00+10", finish_time:"2013-09-03 20:00:00+10"}
    ]

B = [
     {date: "2013-07-31", start_time:"2013-07-31 17:37:49+10", finish_time:"2013-07-31 20:32:04+10"},
     {date: "2013-08-03", start_time:"2013-08-03 16:57:34+10", finish_time:"2013-08-03 20:00:57+10"}
    ]

Expected output
C = [
     {date: "2013-07-31", start_time:"late", finish_time:"on time"},
     {date: "2013-08-03", start_time:"on time", finish_time:"on time"},
    ]

I will compare first if the two object array have the same date then I will compare the start of the same date then if the start_time value on the B exceeds on A start_time then it will change to a string that it is "late". Also for finish_time if the value on B is lower than A then the string would be "too early".

halfer
  • 19,824
  • 17
  • 99
  • 186
Tep Tep
  • 59
  • 3
  • 16
  • So, your two arrays are having the same length, right? Then loop by the length of one of the array, and parse the date, time strings into date or datetime object, so that you can do the comparison easily. Once you got the comparison result, insert the wanted value to your new array. – Vincent Zhang Feb 24 '18 at 04:01
  • Possible duplicate of [How to determine equality for two JavaScript objects?](https://stackoverflow.com/questions/201183/how-to-determine-equality-for-two-javascript-objects) – connexo Feb 24 '18 at 04:02
  • I forgot to mention what if not exact the same length. – Tep Tep Feb 24 '18 at 04:06
  • Is this homework by any chance? – Joseph Feb 24 '18 at 04:26
  • @JosephSerido it's a work. Got problem solved. Thanks! – Tep Tep Feb 24 '18 at 04:37
  • Please read [Under what circumstances may I add “urgent” or other similar phrases to my question, in order to obtain faster answers?](//meta.stackoverflow.com/q/326569) - the summary is that this is not an ideal way to address volunteers, and is probably counterproductive to obtaining answers. Please refrain from adding this to your questions. – halfer Feb 24 '18 at 08:28

3 Answers3

1

To me, this is really just a JavaScript issue, not really an AngularJS one.

I think something like this is what you're looking for:

const A = [
     {date: "2013-07-31", start_time:"2013-07-31 17:30:00+10", finish_time:"2013-07-31 20:30:00+10"},
     {date: "2013-08-03", start_time:"2013-08-03 17:00:00+10", finish_time:"2013-08-03 20:00:00+10"},
     {date: "2013-08-03", start_time:"2013-08-03 17:00:00+10", finish_time:"2013-08-03 20:00:00+10"}
    ];

const B = [
     {date: "2013-07-31", start_time:"2013-07-31 17:37:49+10", finish_time:"2013-07-31 20:32:04+10"},
     {date: "2013-08-03", start_time:"2013-08-03 16:57:34+10", finish_time:"2013-08-03 20:00:57+10"},
     {date: "2013-08-03", start_time:"2013-08-03 16:57:34+10", finish_time:"2013-08-03 19:00:57+10"}
    ];


let C = [];
const maxLength = Math.min(A.length, B.length);

for (let i = 0; i < maxLength; i += 1) {
  const startTimeResult = B[i].start_time > A[i].start_time ? 'late' : 'on time';
  const finishTimeResult = B[i].finish_time > A[i].finish_time ? 'on time' : 'too early';
  C[i] = { date: A[i].date, start_time: startTimeResult, finish_time: finishTimeResult };
  console.log(C[i]);
}

https://codepen.io/joseph4tw/pen/mXGeoO?editors=1012

Joseph
  • 5,070
  • 1
  • 25
  • 26
  • this is good but what if the date on A does not have on B? Can you help me a little. It's now near on my expected output. – Tep Tep Feb 24 '18 at 04:05
  • @TepTep how do I know how to compare A to B? Can I assume that `A[0]` should be compared to `B[0]`, and `A[1]` to `B[1]`? Will `A.length` and `B.length` differ? If so, what should be done? – Joseph Feb 24 '18 at 04:23
  • @TepTep answer updated to account for a difference in array length – Joseph Feb 24 '18 at 04:26
1

From what I understood, basically you want to search for values inside the array B against values present in A, and compute whether the starttimes are early, late or on-time.

The solution by https://stackoverflow.com/users/1148564/joseph-serido will work perfectly, but it will take O(n^2) time. In Javascript, you can access objects by key names. If you make A and B as objects, you can leverage this behavior and make your code run with just one loop.

var A_Times = {}; //Make an Object
//Use the date as Key
A_Obj["2013-07-31"] = {start_time:"2013-07-31 17:30:00+10", finish_time:"2013-07-31 20:30:00+10"};
A_Obj["2013-08-03"] ={ start_time:"2013-08-03 17:00:00+10", finish_time:"2013-08-03 20:00:00+10"};

var B_Times = {};
B_Times["2013-07-31"] = {start_time:"2013-07-31 17:37:49+10", finish_time:"2013-07-31 20:32:04+10"};
B_Times["2013-08-03"] ={start_time:"2013-08-03 16:57:34+10", finish_time:"2013-08-03 20:00:57+10"};

var A_Days = Object.keys(A_Times); //Get all the days in A

for(var i=0; i<A_Times_Days.length; i++){
    var day = A_Times_Days[i];
    console.log(day); //Log the Key here
    var A_Data = A_Times[day];
    var B_Data = B_Times[day];
    console.log(A_Data);
    console.log(B_Data);
    //Compute punctuality here based on A_Data and B_Data
}

However, if your use case involves data for only a few days, maybe this added complexity is not worth it. Upto you to decide the trade-off. Hope it helped.

1

hi this is what i came up with, i don't know exaclty what to say. i think the code will speak for itself :p

var A = [
     {date: "2013-07-31", start_time:"2013-07-31 17:30:00+10", finish_time:"2013-07-31 20:30:00+10"},
     {date: "2013-08-03", start_time:"2013-08-03 17:00:00+10", finish_time:"2013-08-03 20:00:00+10"}
    ];

var B = [
     {date: "2013-07-31", start_time:"2013-07-31 17:37:49+10", finish_time:"2013-07-31 20:32:04+10"},
     {date: "2013-08-03", start_time:"2013-08-03 16:57:34+10", finish_time:"2013-08-03 20:00:57+10"}
     ];

function getResult()
{
    var results = [];
    for(var i = 0; i < A.length; i++)
    {
        var objA = A[i];

        for(var j = 0; j < B.length; j++)
        {
            var objB = B[j];

            if(objB.date === objA.date)
            {
                var o = {};
                o.date = objA.date;

                //if start_time of A is less than start_time of B
                if(Date.parse(objA.start_time) < Date.parse(objB.start_time))
                    o.start_time = "late";
                else
                    o.start_time = "on time";

                //if end_time of A is less than end_time of B
                if(Date.parse(objA.finish_time) < Date.parse(objB.finish_time))
                    o.finish_time = "too early";
                else
                    o.finish_time = "on time";

                results.push(o);
            }
        }
    }

    if(results.length !== 0)
        return results;

    return null;
}

P.S. it will output only objects from where the date of A is equal to the date of B