javascript 如何合并两个对象数组并连接值?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22529691/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 23:18:45  来源:igfitidea点击:

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

javascriptarraysunderscore.jslodash

提问by teone

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.creazionethen 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 yarray (third line of result).

简而言之,我需要检查是否a.creazione == b.creazione然后合并键并连接数组中的值(结果的第一行),否则,如果不同,则在数组的正确位置分配现有数组的值y(第三行结果)。

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

PS:我需要得到这个结构,因为我使用的是Angular-Charts库,它以这种不舒服的方式请求数据。

Any idea on how to achieve this?

关于如何实现这一目标的任何想法?

采纳答案by alex3683

Using plain JavaScript:

使用纯 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/

小提琴: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.

请注意,为了简单起见,修改了订单数组。如果这对您的用例来说是个问题,只需使用切片制作副本。

回答by thefourtheye

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