KodeFor.Me
KodeFor.Me

Reputation: 13511

JavaScript | JSON Objects merge and sort by a value

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

Upvotes: 1

Views: 1058

Answers (2)

Mickael
Mickael

Reputation: 5736

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;
    }
    

Upvotes: 2

ZER0
ZER0

Reputation: 25332

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);

Upvotes: 2

Related Questions