连接和过滤两个数据集

Joining and filtering two data sets

我目前在根据各种条件筛选和加入 2 个数据集时遇到困难。

我收到的数据是这样的

const ob = {
  "dataOne": [
    {
      "type": "Type 1",
      "name": "failed"
    },
    {
      "type": "Type 1",
      "name": "success"
    },
    {
      "type": "Type 2",
      "name": "success"
    },
    {
      "type": "Type 3",
      "name": "success"
    },
  ],
  "dataTwo": [
    {
      "type": "Type 1",
      "name": "click",
    },
    {
      "type": "Type 2",
      "name": "click",
    },
    {
      "type": "Type 2",
      "name": "click",
    },
    {
      "type": "Type 1",
      "name": "click",
    },
    {
      "type": "Type 3",
      "name": "click",
    },
  ]
};

我要的结果是这样

{
    "type 1" : [
        {
            "count": 2
            "success": 1
            "failed": 1
            "clicks": 2
        }
    ],
    "type 2" : [
        {
            "count": 1
            "success": 1
            "failed": 0
            "clicks": 2
        }
    ],
    "type 3" : [
        {
            "count": 1
            "success": 1
            "failed": 0
            "clicks": 1
        }
    ]
}

所以我的主要目标是从 dataOne 中获取唯一类型。然后我需要每种类型的总出现次数。然后,我需要知道每种类型的成功和失败总数。

然后从 dataTwo 我需要包括每种类型的点击次数。

开始(不确定我的方向是否正确),我已经完成了

const result = ob.dataOne.reduce( (acc, o) => (acc[o.type] = (acc[o.type] || 0)+1, acc), {} );

这会给出每种类型的总计数。但是,我不确定如何检查成功和失败并像上面那样格式化。

任何建议表示赞赏。我开始了 JSFiddle

谢谢

我认为先连接两个数据集再归约更容易。这是我的代码:

const ob = {
  "dataOne": [
    {
      "type": "Type 1",
      "name": "failed"
    },
    {
      "type": "Type 1",
      "name": "success"
    },
    {
      "type": "Type 2",
      "name": "success"
    },
    {
      "type": "Type 3",
      "name": "success"
    },
  ],
  "dataTwo": [
    {
      "type": "Type 1",
      "name": "click",
    },
    {
      "type": "Type 2",
      "name": "click",
    },
    {
      "type": "Type 2",
      "name": "click",
    },
    {
      "type": "Type 1",
      "name": "click",
    },
    {
      "type": "Type 3",
      "name": "click",
    },
  ]
};

const concatenatedArrays = ob.dataOne.concat(ob.dataTwo);

const result = concatenatedArrays.reduce((acc, o) => {
  if (!acc[o.type]) {
    acc[o.type] = [{
      "count": 0,
      "success": 0,
      "failed": 0,
      "click": 0,
    }];
  }
  if(o.name == "failed" || o.name == "success"){
    acc[o.type][0]['count'] = (acc[o.type][0]['count'] || 0) + 1;

  }

  acc[o.type][0][o.name] =  acc[o.type][0][o.name] +1

  return acc;
}, {}); 

console.log(result);

编辑:

嗯所以计数只是针对 dataOne,更改代码并使用 reduce 两次:

const ob = {
  "dataOne": [
    {
      "type": "Type 1",
      "name": "failed"
    },
    {
      "type": "Type 1",
      "name": "success"
    },
    {
      "type": "Type 2",
      "name": "success"
    },
    {
      "type": "Type 3",
      "name": "success"
    },
  ],
  "dataTwo": [
    {
      "type": "Type 1",
      "name": "click",
    },
    {
      "type": "Type 2",
      "name": "click",
    },
    {
      "type": "Type 2",
      "name": "click",
    },
    {
      "type": "Type 1",
      "name": "click",
    },
    {
      "type": "Type 3",
      "name": "click",
    },
  ]
};

const dataOneReduced = ob.dataOne.reduce((acc, o) => {
  if (!acc[o.type]) {
    acc[o.type] = [
      {
        count: 0,
        success: 0,
        failed: 0,
        click: 0,
      },
    ];
  }

  acc[o.type][0]["count"] = (acc[o.type][0]["count"] || 0) + 1;
  acc[o.type][0][o.name] = acc[o.type][0][o.name] + 1;

  return acc;
}, {});

const result = ob.dataTwo.reduce((acc, o) => {
  acc[o.type][0][o.name] = acc[o.type][0][o.name] + 1;

  return acc;
}, dataOneReduced);

console.log(result);