收藏
回答

数组合并

源代码:

var j1 = [

{

"cinema_movie_id": 1,

"cinema_id": "1",

"num": 3

},

{

"cinema_movie_id": 2,

"cinema_id": "0",

"pric":"40"

},

{

"cinema_movie_id": 2,

"cinema_id": "0",

"num": 6

}

]

var jmap = {};

var result = [];


j1.forEach(function (al) {

var key = al.cinema_movie_id + '_' + al.cinema_id;

if (typeof jmap[key] === 'undefined') {

jmap[key] = [];

}

jmap[key].push(al);

})


var keys = Object.keys(jmap);

for (var i = 0; i < keys.length; i++) {

var rs = keys[i].split('_');

result.push({

cinema_movie_id: rs[0],

cinema_id: rs[1],

value: jmap[keys[i]]

});

}


怎么把这两个合并在一块?已经通过cinema_id分好组了,然后把相同的cinema_id合并在一块


回答关注问题邀请回答
收藏

1 个回答

  • 佳梨
    佳梨
    2019-02-25

    j1.forEach(function (al) {

        var key = al.cinema_movie_id + '_' + al.cinema_id;

        jmap[key] = jmap[key] ? {
            ...jmap[key],
            ...al

        } : al;

    })


    2019-02-25
    有用
    回复
登录 后发表内容