1

I have the following json abjects, and I like to merge them into one object and the short the sub-objects based on 'minute' value. Is that posible with JavaScript ?

[Object, Object, Object]
    0: Object
        Live: "188585"
        Minute: "17"
        Player: "Player A"
        Team: "188564"
        __proto__: Object
    1: Object
        Live: "188585"
        Minute: "26"
        Player: "Player B"
        Team: "188564"
        __proto__: Object
    2: Object
        Live: "188585"
        Minute: "77"
        Player: "Player A"
        Team: "188564"
        __proto__: Object
[Object, Object]
    0: Object
        Live: "188585"
        Minute: "31"
        Player: "Player C"
        Team: "188558"
        __proto__: Object
    1: Object
        Live: "188585"
        Minute: "41"
        Player: "Player D"
        Team: "188558"
        __proto__: Object

the result I like to be like that:

[Object, Object, Object, Object, Object]
    0: Object
        Live: "188585"
        Minute: "17"
        Player: "Player A"
        Team: "188564"
        __proto__: Object
    1: Object
        Live: "188585"
        Minute: "26"
        Player: "Player B"
        Team: "188564"
        __proto__: Object
    2: Object
        Live: "188585"
        Minute: "31"
        Player: "Player C"
        Team: "188558"
        __proto__: Object
    3: Object
        Live: "188585"
        Minute: "41"
        Player: "Player D"
        Team: "188558"
        __proto__: Object
    4: Object
        Live: "188585"
        Minute: "77"
        Player: "Player A"
        Team: "188564"
        __proto__: Object

2 Answers 2

2

More then objects they seems arrays of objects. If it's that the case, you can use Array.concat to concatenate, and Array.sort to sort.

Something like:

// assuming `aba` is the first array, `cd` the second
var result = aba.concat(cd);

result.sort(function(a, b) {
    return a.Minute - b.Minute // implicit conversion in number
});

console.log(result);
Sign up to request clarification or add additional context in comments.

Comments

2

If your two arrays are already sorted by minute, you can use the "classical" merge algorithms. If not, you can concat your arrays and sort the result using Array.sort (with a specific compare function).

Edit: here is an example:

  1. If arrays are not sorted:

    var result = array1.concat(array2);
    result.sort(function(item1, item2) {
        return item1.Minute - item2.Minute;
    });
    
  2. If arrays are sorted, here is a simple merge function:

    function merge(array1, array2) {
        var results = [];
        var i = 0, j = 0;
        while (i < array1.length && j < array2.length) {
            if (array1[i].Minute <= array2[j].Minute) {
                results.push(array1[i];
                i++;
            }
            else {
                results.push(array2[j];
                j++;
            }
        }
        while (i < array1.length) {
            results.push(array1[i];
            i++;
        }
        while (j < array2.length) {
            results.push(array2[j];
            j++;
        }
        return results;
    }
    

1 Comment

can you provide me with a simple example ?

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.