如何按月然后按客户将一系列产品销售分组

吉氏酵母

一年中,我为许多客户存储了一系列不同的产品销售,每个月都有存储。我希望能够将一年内购买的客户分组为一个列。

var sales = [
  ["Jan", "Customer 1", "Product 1", 538]
  ["Feb", "Customer 1", "Product 1", 0]
  ["March", "Customer 1", "Product 1", 252]
  ["Apr", "Customer 1", "Product 1", 0]
  ["May", "Customer 1", "Product 1", 630]
  ["June", "Customer 1", "Product 1", 945]
  ["July", "Customer 1", "Product 1", 0]
  ["Aug", "Customer 1", "Product 1", 630]
  ["Sept", "Customer 1", "Product 1", 756]
  ["Oct", "Customer 1", "Product 1", 0]
  ["Nov", "Customer 1", "Product 1", 0]
  ["Dec", "Customer 1", "Product 1", 630]
  ["Jan", "Customer 1", "Product 2", 50]
  ["Feb", "Customer 1", "Product 2", 630]
  ...
]

我尝试将独特的产品分组,然后获得独特的客户,然后获得独特的产品,如下所示,但我不知道如何从这里继续前进。

// Get a list of unique products
var products = sales.map(item => {
  return item[2];
}).filter((val, index, self) => {
  return self.indexOf(val) === index;
});
console.log(products);

// Ge a list of unique customers
var targets = sales.map(item => {
  return item[1]
}).filter((val, index, self) => {
  return self.indexOf(val) === index
});
console.log(targets);

我想要一组按客户和产品分组的对象

[
  {
    "customer": "Customer 1",
    "product": "Product 1",
    "jan" 538,
    "feb": 0,
    "march": 252
    ...
  },
  {
    "customer": "Customer 1",
    "product": "Product 2",
    "jan" 50,
    "feb": 630,
    ...
  }
]

有任何想法吗?

妮娜·斯科茨(Nina Scholz)

您可以使用来自客户和产品的组合键,然后为该对象获取哈希表。最后,从哈希表中获取这些值。

var sales = [["Jan", "Customer 1", "Product 1", 538], ["Feb", "Customer 1", "Product 1", 0], ["March", "Customer 1", "Product 1", 252], ["Apr", "Customer 1", "Product 1", 0], ["May", "Customer 1", "Product 1", 630], ["June", "Customer 1", "Product 1", 945], ["July", "Customer 1", "Product 1", 0], ["Aug", "Customer 1", "Product 1", 630], ["Sept", "Customer 1", "Product 1", 756], ["Oct", "Customer 1", "Product 1", 0], ["Nov", "Customer 1", "Product 1", 0], ["Dec", "Customer 1", "Product 1", 630], ["Jan", "Customer 1", "Product 2", 50], ["Feb", "Customer 1", "Product 2", 630]],
    result = Object.values(sales.reduce((r, [month, customer, product, value]) => {
        var key = [customer, product].join('|');
        r[key] = r[key] || { customer, product };
        r[key][month.toLowerCase()] = value;
        return r;
    }, {}));

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

一系列产品

动态地将一系列产品ID传递给Woocommerce“产品”简码

如何汇总客户,按性别然后按月分组?

如何在Hyperledger Composer中使用单元测试引用一系列产品

如果购物车包含一系列产品,如何从特定标签计算 WooCommerce 产品

如何按熊猫的一系列值分组?

如何按年然后按月对日期列表进行分组?

如何在Excel中按年份然后按月份对列排序进行分组?

Google Charts API-如何将我的x轴标签转换为日期格式,然后按一下按钮按月分组

如何将一系列 str 列表与一系列 str 连接起来

如何将一系列参数应用于 MATLAB 中的一系列函数?

如何检查客户是否提出了保修索赔和产品销售?

如何测试一系列互动?

如何减少一系列if语句?

如何串联一系列细胞

如何打印一系列纸张?

如何制作一系列物品

如何打印一系列因素的所有非重复产品

拟合一系列线性模型以将因变量分开以用于不同的分组变量

将Pandas DataFrame转换为字典时,嵌套/分组一系列列

如何使用 audioread 读取一系列文件,然后将它们组合起来?

熊猫:如何选择按键分组的一系列行的最小值

我如何使用 ifelse 对一系列变量进行分组

监视一系列事件,然后做出决定

如何提取一系列数字以及单个字母,然后再提取另一系列数字?

对 Panda Dataframe 中的一系列列表进行分组

使用一系列条件语句分组

将一系列分配给dataFrame的所有列(按列)?

如何有效地找到一系列数字的按位或