数组合并
发布于 6 年前 作者 jie65 13146 次浏览 来自 问答

源代码:

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 回复

j1.forEach(function (al) {

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

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

    } : al;

})
回到顶部