Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I merge two array of objects and concatenate values?

I have this two array of objects

var client = [{
        "creazione": "1970-01-01",
        "value": 2
    }, {
        "creazione": "2014-03-12",
        "value": 4
    }, {
        "creazione": "2014-03-14",
        "value": 1
    }],
    order = [{
        "creazione": "1970-01-01",
        "value": 1
    }, {
        "creazione": "2014-03-13",
        "value": 5
    }, {
        "creazione": "2014-03-14",
        "value": 1
    }];

I need to merge these two arrays to get something like:

 [{
     x: '1970-01-01',
     y: [2, 1]
 }, {
     x: '2014-03-12',
     y: [4, 0]
 }, {
     x: '2014-03-14',
     y: [1, 1]
 }, {
     x: '2014-03-13',
     y: [0, 5]
 }]

In few words I need to check if a.creazione == b.creazione then merge the key and concat the values in an array (first line of result), else if it is different, assign the value of the existing array in the right position of the y array (third line of result).

PS: I need to get this structure beacuse I'm using Angular-Charts library, and it ask for data in this uncomfortable way.

Any idea on how to achieve this?

like image 287
teone Avatar asked Mar 20 '14 10:03

teone


2 Answers

Allow me to amuse you with the power of functional programming :)

client = _.object(_.map(client, _.values));
order  = _.object(_.map(order , _.values));
var result = _
    .chain(_.keys(client))
    .union(_.keys(order))
    .map(function (key) {
        return [key, [client[key] || 0, order[key] || 0]];
    })
    .map(_.partial(_.zipObject, ['x', 'y']))
    .value();
console.log(result);
# [ { x: '1970-01-01', y: [ 2, 1 ] },
#   { x: '2014-03-12', y: [ 4, 0 ] },
#   { x: '2014-03-14', y: [ 1, 1 ] },
#   { x: '2014-03-13', y: [ 0, 5 ] } ]
like image 97
thefourtheye Avatar answered Oct 21 '22 00:10

thefourtheye


Using plain JavaScript:

var result = [];
client.forEach( function( entry ) {
    for( var i = 0; i < order.length; ++i ) {
        if( entry.creazione === order[i].creazione ) {
            result.push( { x: entry.creazione, y: [ entry.value, order[i].value ] } );
            order.splice( i, 1 );
            return;
        }
    }
    result.push( { x: entry.creazione, y: [ entry.value, 0 ] } );
} );
order.forEach( function( entry ) {
    result.push( { x: entry.creazione, y: [ 0, entry.value ] } );
} );

Fiddle: http://jsfiddle.net/rPk6e/

Note that for simplicity the order array is modified. If that is a problem for your use case simply make a copy using slice.

like image 24
alex3683 Avatar answered Oct 20 '22 23:10

alex3683