2017-10-21 27 views
-1

私は2つの配列があります。マージ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} 
] 

私はこの上で失われたビットです。誰にでも助けてくれてとても感謝しています。

ありがとうございます。

+0

https://www.w3schools.com/jsref/のような配列の機能を試してみてくださいjsref_concat_array.asp –

答えて

2

最初にカウントに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を見ることができます:https://jsfiddle.net/z4sdcuku/

1

あなたはMapを使用し、最初のすべてのカウントを取ることができます。次に、新しいオブジェクトをマップします。

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

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

JSFiddle

関連する問題