1

I have the following two arrays in 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.

Dave
  • 161
  • 4
  • 8
  • 2
    Show the `I've tried multiple different solutions, looping through both arrays` part - you might be closer than you think – Ian Jul 11 '13 at 14:39
  • What was the last thing you've tried? – Ja͢ck Jul 11 '13 at 14:40
  • So the arrays are sorted every time? – Bergi Jul 11 '13 at 14:50
  • 2
    *"I can't seem to figure out an elegant solution"* So you did figure out a solution, but it's not "elegant" enough for you? If so, then how are we going to know if any of our solutions are any better? –  Jul 11 '13 at 14:54

3 Answers3

1

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/

ElmoVanKielmo
  • 10,907
  • 2
  • 32
  • 46
0

A simple solution for n arrays:

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). If you've got only two lists, have a look at Most efficient way to merge two arrays of objects.

Community
  • 1
  • 1
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
0
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; 
}
tomdemuyt
  • 4,572
  • 2
  • 31
  • 60