将两个数组对比后合并为同一个数组

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"}]
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值