let arr = [{week:"1"},
{week:"2"},
{week:"3"},
{week:"4"},
{week:"5"},
{week:"6"},
{week:"7"},
{week:"8"},
{week:"9"},
{week:"10"},
{week:"11"},
{week:"12"},
{week:"13"}]
let bb = [
{week:"2",tongbi:"234"},
{week:"5",tongbi:"54"}];
const composeData = (target,source) => {
return target.map(item => {
const findItem = source.find(el => el.week === item.week);//识别week值是否相等,如相等,findItem等于当前el
return findItem || Object.assign(item,{tongbi:'0'})
})
}
console.log(composeData(arr,bb))
// 目标
// [{week:"1",tongbi:"0"},
// {week:"2",tongbi:"234"},
// {week:"3",tongbi:"0"},
// {week:"4",tongbi:"0"},
// {week:"5",tongbi:"54"},
// {week:"6",tongbi:"0"},
// {week:"7",tongbi:"0"},
// {week:"8",tongbi:"0"},
// {week:"9",tongbi:"0"},
// {week:"10",tongbi:"0"},
// {week:"11",tongbi:"0"},
// {week:"12",tongbi:"0"},
// {week:"13",tongbi:"0"}]