扫描嵌套对象的数组以进行匹配

时间:2017-04-04 23:36:03

标签: javascript arrays javascript-objects

我有一个看起来像这样的对象 -

{
"Campaigns":
{
"Campaign1":
{
    "reports":
        [
            {
                "month": "Google - January - Monthly Report.csv",
                "impressions": 53,
                "clicks": 31,
                "cost": 18
            },
            {
                "month": "Google - April - Monthly Report.csv",
                "impressions": 13,
                "clicks": 11,
                "cost": 8
            }
        ]
    },
"Campaign2":
{
    "reports":
        [
            {
                "month": "Google - March - Monthly Report.csv",
                "impressions": 13,
                "clicks": 11,
                "cost": 8
            }
        ]
}
}

}

我还有一个带文件名的附加数组..让我们以这个数组为例 -

reports_array = [
                 'Google - January - Monthly Report.csv',
                 'Google - February - Monthly Report.csv',
                 'Google - March - Monthly Report.csv',
                 'Google - April - Monthly Report.csv'
                 ]

我希望完成的是在每个广告系列中扫描“缺失”报告(在本例中为“广告系列1”和“广告系列2”),并在所有属性中推送包含零的新报告(展示次数,点击次数,费用)因此,除了1月和4月的报告之外,campaign1还将有2月和3月的报告,只有零而不是真实的数据。而campaign2将有另外三个报告(1月,2月和4月),还有零而不是真实数据。

到目前为止我尝试了什么(没有取得多大成功) -

reports_array.forEach(function(arr_report) {
    for(let campaign in campaigns) {
        if(campaigns[campaign].hasOwnPropert('reports')) {
            campaigns[campaign].reports.forEach(function(report) {
                if(report.month !== arr_report) {
                    console.log('campaign '+campaign+' is missing '+arr_report);
                    campaigns[campaign].reports.push({
                       month: arr_report,
                       impressions: 0,
                       clicks: 0,
                       cost: 0
                    });
                }
            });
        }
    }
});

2 个答案:

答案 0 :(得分:1)

这是一种方法:

[.numeric, .caseInsensitive]

给你这个结果:

var campaigns = campaigns_object.Campaigns;

Object.keys(campaigns)
  .forEach(function(campaign) {
    reports_array.forEach(function(report, index) {
      var has_report = campaigns[campaign].reports.some(function(campaign_report) {
        return (campaign_report.month === report);
      });

      if (!has_report) {
        campaigns[campaign].reports.splice(index, 0, {
            month: report,
          impressions: 0,
          clicks: 0,
          cost: 0
        });
      }
    });
  });

console.log(campaigns);

JSFiddle演示:https://jsfiddle.net/wfya0ycs/3/

答案 1 :(得分:1)

function fill(campaigns, reports) {
  for(var key in campaigns) {                                  // for each campaign in the campaign object
    reports.filter(function(report) {                          // filter out the missing reports
      return campaigns[key].reports.every(function(creport) {  // if every report in this campaign's reports array is not equal to this report, then it's is considered missing
        return creport.month !== report;
      });
    }).forEach(function(report) {                              // then for each report in the missing reports array
      campaigns[key].reports.push({                            // push a new blank report
        "month": report,
        "impressions": 0,
        "clicks": 0,
        "cost": 0
      });
    });
  }
}

var campaigns_object = {"Campaigns":{"Campaign1":{"reports":[{"month":"Google - January - Monthly Report.csv","impressions":53,"clicks":31,"cost":18},{"month":"Google - April - Monthly Report.csv","impressions":13,"clicks":11,"cost":8}]},"Campaign2":{"reports":[{"month":"Google - March - Monthly Report.csv","impressions":13,"clicks":11,"cost":8}]}}},
    reports_array = ["Google - January - Monthly Report.csv","Google - February - Monthly Report.csv","Google - March - Monthly Report.csv","Google - April - Monthly Report.csv"];

fill(campaigns_object.Campaigns, reports_array);

console.log(campaigns_object);