简体   繁体   English

结合JavaScript中两个多维数组中的元素

[英]Combine elements from two multidimensional arrays in JavaScript

I have the following two arrays in JavaScript: 我在JavaScript中有以下两个数组:

"total":[[1370923200000,"66"],[1371009600000,"42"],[1371096000000,"23"]]

"successful":[[1370923200000,"5"],[1371096000000,"2"],[1371182400000,"0"]]

I'd like to combine them into one array / object which looks something like this: 我想将它们组合成一个看起来像这样的数组/对象:

{date:1370923200000, total:"66", successful:"5"},
{date:1371009600000, total:"42"},
{date:1371096000000, total:"23", successful:"2"},
{date:1371182400000, successful:"0"}

I've tried multiple different solutions, looping through both arrays, but I can't seem to figure out an elegant solution. 我尝试了多种不同的解决方案,遍历这两个数组,但似乎无法找出一种优雅的解决方案。

Here you have: 这里您有:

var total = [[1370923200000, "66"],[1371009600000, "42"],[1371096000000, "23"]];
var successful = [[1370923200000, "5"],[1371096000000, "2"],[1371182400000, "0"]];
var combined = {};

for(var i=0; i<total.length; i++){
    combined[total[i][0]] = {date: total[i][0], total: total[i][1]};
}

for(var i=0; i<successful.length; i++){
    if(successful[i][0] in combined){
        combined[successful[i][0]].successful = successful[i][1];
    }
    else{
        combined[successful[i][0]] = {
            date: successful[i][0], successful: successful[i][1]
        };
    }
}

var result = [];
for(var key in combined){
    result.push(combined[key]);
}
alert(result.toSource());

And a working fiddle http://jsfiddle.net/eRjeZ/ 和一个工作的小提琴http://jsfiddle.net/eRjeZ/

A simple solution for n arrays: n个数组的简单解决方案:

var arrays = {"total":[…], "successful":[…]};

var result = [];
for (var prop in arrays) {
    var arr = arrays[prop];
    var i=0;
    for (var j=0; j<arr.length; j++) {
        var date = arr[j][0];
        while (i < result.length && date > result[i].date) i++;
        if (i < result.length && date == result[i].date) {
            result[i][prop] = arr[j][1];
        } else {
            var o = {date:date};
            o[prop] = arr[j][1];
            result.splice(i, 0, o);
        }
    }
}

If you need it faster, you might use the Multiple Array Merge Using Binary Heap (see also Algorithm for N-way merge ). 如果需要更快的速度,则可以使用“ 使用二进制堆进行多阵列合并” (另请参阅“ N路合并算法” )。 If you've got only two lists, have a look at Most efficient way to merge two arrays of objects . 如果只有两个列表,请查看合并两个对象数组的最有效方法

var total = [[1370923200000, "66"],[1371009600000, "42"],[1371096000000, "23"]];
var succes = [[1370923200000, "5"],[1371096000000, "2"],[1371182400000, "0"]];
var everything = {}; 
var output = [];

total.map(  function( item ){ addToEverything( item , "total" ) } );
succes.map( function( item ){ addToEverything( item , "successful" ) } );

console.log( everything ); // This looks 'like' what you want, but better

for( var key in everything )
  output.push( everything[key] );

console.log( output ); //This looks exactly like what you want

function addToEverything( item , name )
{
  var key = item[0];
  var entry = everything[key] || { date : key };
  entry[name] = item[1];
  everything[key] = entry; 
}

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

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