合并2个javascript数组

合并2个javascript数组,javascript,arrays,json,Javascript,Arrays,Json,我有两个阵列: const calendar = [ {"_id":"Jan"}, {"_id":"Feb"}, {"_id":"Mar"}, {"_id":"Apr"}, {"_id":"May"}, {"_id":"Jun"}, {"_id":"Jul"}, {"_id":"Aug"}, {"_id":"Sep"}, {"_id":"Oct"}, {"_id":"Nov"}, {"_id":"Dec"} ] 及 我希望合并这两个数组,以便在当月没有计数时,将

我有两个阵列:

const calendar = [
    {"_id":"Jan"}, {"_id":"Feb"}, {"_id":"Mar"},
    {"_id":"Apr"}, {"_id":"May"}, {"_id":"Jun"},
    {"_id":"Jul"}, {"_id":"Aug"}, {"_id":"Sep"},
    {"_id":"Oct"}, {"_id":"Nov"}, {"_id":"Dec"}
]

我希望合并这两个数组,以便在当月没有计数时,将其设置为“计数”:0

例如,新阵列应如下所示:

const final = [
    {"_id":"Jan","count":1}, {"_id":"Feb","count":1},
    {"_id":"Mar","count":2}, {"_id":"Apr","count":6},
    {"_id":"May","count":5}, {"_id":"Jun","count":2},
    {"_id":"Jul","count":1}, {"_id":"Aug","count":0},
    {"_id":"Sep","count":0}, {"_id":"Oct","count":0},
    {"_id":"Nov","count":0}, {"_id":"Dec","count":0}
]
我对此有点迷茫。非常感谢任何人的帮助


谢谢

首先创建一个ID到计数的映射。然后将所有日历月映射到已创建映射中的计数,如果不存在,则默认为0

var countMap = {};
count.forEach((a) => {
    countMap[a._id] = a.count
});

const final = calendar.map((month) => ({_id: month._id, count: countMap[month._id] ||0}))
您可以在此处看到一个正在工作的exmaple:

您可以使用a并首先进行所有计数。然后映射新对象

var calendar=[{id:“一月”}、{id:“二月”}、{id:“三月”}、{id:“四月”}、{id:“五月”}、{id:“六月”}、{id:“七月”}、{id:“八月”}、{id:“九月”}、{id:“十月”}、{id:“十一月”}、{id:“十二月”},
count=[{id:“一月”,count:1},{id:“四月”,count:6},{id:“五月”,count:5},{id:“二月”,count:1},{id:“七月”,count:1},{id:“三月”,count:2},{id:“六月”,count:2}],
map=newmap(count.map(o=>[o.\u id,o.count]),
final=calendar.map(o=>Object.assign({},o,{count:map.get(o.|u id)| 0}));
控制台日志(最终)

.as控制台包装{max height:100%!important;top:0;}
这将为您提供:

const calendar = [{
  "_id": "Jan"
}, {
  "_id": "Feb"
}, {
  "_id": "Mar"
}, {
  "_id": "Apr"
}, {
  "_id": "May"
}, {
  "_id": "Jun"
}, {
  "_id": "Jul"
}, {
  "_id": "Aug"
}, {
  "_id": "Sep"
}, {
  "_id": "Oct"
}, {
  "_id": "Nov"
}, {
  "_id": "Dec"
}];

const count = [{
  "_id": "Jan",
  "count": 1
}, {
  "_id": "Apr",
  "count": 6
}, {
  "_id": "May",
  "count": 5
}, {
  "_id": "Feb",
  "count": 1
}, {
  "_id": "Jul",
  "count": 1
}, {
  "_id": "Mar",
  "count": 2
}, {
  "_id": "Jun",
  "count": 2
}]

var result = [];
for (var mth = 0; mth < calendar.length; mth++) {
  var ct = 0;
  for (var mthCt = 0; mthCt < count.length; mthCt++) {
    if (calendar[mth]._id === count[mthCt]._id) {
      ct = count[mthCt].count;
    }
  }
  result[mth] = {
    "id": calendar[mth]._id,
    "count": ct
  }
}
document.getElementById('output').textContent = JSON.stringify(result);
const日历=[{
“_id”:“Jan”
}, {
_id:“二月”
}, {
“_id”:“Mar”
}, {
_id:“Apr”
}, {
“_id”:“May”
}, {
_id:“Jun”
}, {
“_id”:“七月”
}, {
_id:“八月”
}, {
“_id”:“Sep”
}, {
_id:“十月”
}, {
“\u id”:“11月”
}, {
_id:“Dec”
}];
常数计数=[{
“_id”:“Jan”,
“计数”:1
}, {
“_id”:“Apr”,
“计数”:6
}, {
“_id”:“May”,
“计数”:5
}, {
“_id”:“Feb”,
“计数”:1
}, {
“_id”:“七月”,
“计数”:1
}, {
“_id”:“Mar”,
“计数”:2
}, {
“_id”:“Jun”,
“计数”:2
}]
var结果=[];
对于(var mth=0;mth

请尝试使用数组函数,如
const calendar = [{
  "_id": "Jan"
}, {
  "_id": "Feb"
}, {
  "_id": "Mar"
}, {
  "_id": "Apr"
}, {
  "_id": "May"
}, {
  "_id": "Jun"
}, {
  "_id": "Jul"
}, {
  "_id": "Aug"
}, {
  "_id": "Sep"
}, {
  "_id": "Oct"
}, {
  "_id": "Nov"
}, {
  "_id": "Dec"
}];

const count = [{
  "_id": "Jan",
  "count": 1
}, {
  "_id": "Apr",
  "count": 6
}, {
  "_id": "May",
  "count": 5
}, {
  "_id": "Feb",
  "count": 1
}, {
  "_id": "Jul",
  "count": 1
}, {
  "_id": "Mar",
  "count": 2
}, {
  "_id": "Jun",
  "count": 2
}]

var result = [];
for (var mth = 0; mth < calendar.length; mth++) {
  var ct = 0;
  for (var mthCt = 0; mthCt < count.length; mthCt++) {
    if (calendar[mth]._id === count[mthCt]._id) {
      ct = count[mthCt].count;
    }
  }
  result[mth] = {
    "id": calendar[mth]._id,
    "count": ct
  }
}
document.getElementById('output').textContent = JSON.stringify(result);