简体   繁体   English

JavaScript | JSON对象按值合并和排序

[英]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. 我有以下json abjects,我喜欢将它们合并到一个对象中,并根据'minute'值缩短子对象。 Is that posible with JavaScript ? 这是可以用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

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). 如果没有,您可以使用Array.sort(使用特定的比较函数)连接数组并对结果进行排序。

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

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. 如果是这种情况,可以使用Array.concat进行连接,并使用Array.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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM