我有兩個數組:合併2 JavaScript數組
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}
]
我想兩個數組等合併,當有該月沒有計數,使其"count":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}
]
我有點失去了對這個。非常感謝任何人的幫助。
感謝
請嘗試使用陣列功能試驗樣https://www.w3schools.com/jsref/ jsref_concat_array.asp –