简体   繁体   中英

merging 2 javascript arrays

I have two arrays:

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

and

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

I would like to merge the two arrays and so that when there are no counts for that month, make it "count":0 .

For example the new array should look like this:

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

I'm a bit lost on this. Would be very grateful for anyones assistance.

Thanks

First create a map of the ids to the count. then map all calendar months to the count from the created map and default to 0 if no such exists.

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

const final = calendar.map((month) => ({_id: month._id, count: countMap[month._id] ||0}))

you can see a working exmaple here: https://jsfiddle.net/z4sdcuku/

You could use a Map and take all counts first. Then map the new objects.

 var 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" }], 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 }], map = new Map(count.map(o => [o._id, o.count])), final = calendar.map(o => Object.assign({}, o, { count: map.get(o._id) || 0 })); console.log(final); 
 .as-console-wrapper { max-height: 100% !important; top: 0; } 

This will do it for you :

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);

JSFiddle

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM