如何对像 Excel Pivot 这样两个键必须匹配的数组求和?

2024-05-12

我尝试对“Datum”和“Material”必须匹配的所有“Menge”和“Fehler”值求和。结果应类似于 Excel 数据透视表。

到目前为止,这是我的代码,但我不知道如何添加也必须匹配的第二个键“Material”。 我希望你能理解我试图解释的内容。

var arr = [{
  "Datum": {
    "date": "2000-01-01 00:00:00.000000",
    "timezone_type": 3,
    "timezone": "Europe/Berlin"
  },
  "Material": "123",
  "Menge": 100,
  "Fehler": 5
}, {
  "Datum": {
    "date": "2000-01-01 00:00:00.000000",
    "timezone_type": 3,
    "timezone": "Europe/Berlin"
  },
  "Material": "123",
  "Menge": 5,
  "Fehler": 1
}, {
  "Datum": {
    "date": "2000-01-01 00:00:00.000000",
    "timezone_type": 3,
    "timezone": "Europe/Berlin"
  },
  "Material": "123",
  "Menge": 6,
  "Fehler": 65
}, {
  "Datum": {
    "date": "2000-01-01 00:00:00.000000",
    "timezone_type": 3,
    "timezone": "Europe/Berlin"
  },
  "Material": "222",
  "Menge": 10,
  "Fehler": 5
}, {
  "Datum": {
    "date": "2000-01-02 00:00:00.000000",
    "timezone_type": 3,
    "timezone": "Europe/Berlin"
  },
  "Material": "444",
  "Menge": 29,
  "Fehler": 1
}, {
  "Datum": {
    "date": "2000-01-02 00:00:00.000000",
    "timezone_type": 3,
    "timezone": "Europe/Berlin"
  },
  "Material": "123",
  "Menge": 1,
  "Fehler": 1
}]

const result = Object.values(arr.reduce((acc, obj) => {
  const [Datum] = obj.Datum.date.split(' ');
  const Menge = (acc[Datum] ?.Menge + obj.Menge) || obj.Menge;
  const Fehler = (acc[Datum] ?.Fehler + obj.Fehler) || obj.Fehler;
  acc[Datum] = {
    Datum,
    Menge,
    Fehler
  };
  return acc;
}, {}));

console.log(result)

但结果应该是这样的:

[{
    "Datum": "2000-01-01",
    "Material": "123",
    "Menge": 111,
    "Fehler": 71
  },
  {
    "Datum": "2000-01-01",
    "Material": "222",
    "Menge": 10,
    "Fehler": 5
  },
  {
    "Datum": "2000-01-02",
    "Material": "444",
    "Menge": 29,
    "Fehler": 1
  },
  {
    "Datum": "2000-01-02",
    "Material": "123",
    "Menge": 1,
    "Fehler": 1
  }
]

非常感谢


您需要一个组合钥匙Datum and Material.

const
    data = [{ Datum: { date: "2000-01-01 00:00:00.000000", timezone_type: 3, timezone: "Europe/Berlin" }, Material: "123", Menge: 100, Fehler: 5 }, { Datum: { date: "2000-01-01 00:00:00.000000", timezone_type: 3, timezone: "Europe/Berlin" }, Material: "123", Menge: 5, Fehler: 1 }, { Datum: { date: "2000-01-01 00:00:00.000000", timezone_type: 3, timezone: "Europe/Berlin" }, Material: "123", Menge: 6, Fehler: 65 }, { Datum: { date: "2000-01-01 00:00:00.000000", timezone_type: 3, timezone: "Europe/Berlin" }, Material: "222", Menge: 10, Fehler: 5 }, { Datum: { date: "2000-01-02 00:00:00.000000", timezone_type: 3, timezone: "Europe/Berlin" }, Material: "444", Menge: 29, Fehler: 1 }, { Datum: { date: "2000-01-02 00:00:00.000000", timezone_type: 3, timezone: "Europe/Berlin" }, Material: "123", Menge: 1, Fehler: 1 }],
    result = Object.values(data.reduce((acc, { Datum: { date }, Material, Menge, Fehler }) => {
        const
            Datum = date.slice(0, 10),
            key = [Datum, Material].join('|');

        acc[key] ??= { Datum, Material, Menge: 0, Fehler: 0 };
        acc[key].Menge += Menge,
        acc[key].Fehler += Fehler;

        return acc;
    }, {}));

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何对像 Excel Pivot 这样两个键必须匹配的数组求和? 的相关文章

随机推荐

  • BigDecimal 的 JPA @Size 注释

    我该如何使用 SizeMySQL 的注释DECIMAL x y 列 我在用着BigDecimal 但是当我尝试包括 Size max它不起作用 这是我的代码 Size max 7 2 Column name weight private B
  • 从命令行获取 NuGet.exe 版本

    我想得到NuGet exe使用命令行的版本 我知道我可以按照说明获取作为 Visual Studio 一部分运行的 NuGet 版本here http docs nuget org docs start here nuget faq How
  • DynamoDB 中的时间戳应使用什么数据类型?

    我是 DynamoDB 新手 我希望创建一个使用 DeviceID 作为哈希键 时间戳作为范围键和一些数据的表 DeviceID 123 Timestamp 2016 11 11T17 21 07 5272333Z X 12 Y 35 在
  • STM32F4 板上的 SPI 从机设置

    我正在尝试通过主从配置中的 SPI 在两个 STM32F4 发现板之间进行通信 我已经有了主设备的代码 但我对需要对从设备的 SPI 初始化进行的更改感到有点困惑 我还想在主机发送数据时实现中断 而不是让从机一直轮询 RXNE 寄存器 但是
  • 如何屏蔽 Protobuf 中的某些字段

    我找不到一种方法来屏蔽 protobuf 结构中的某些字段 我确实阅读了有关 FieldMaskUtil 的内容并尝试了几个示例 但它似乎做了相反的操作 即复制 FieldMask 中提到的字段 这与我想要的不同 这是示例结构和相应的测试代
  • java.lang.RuntimeException:release()后调用的方法

    If i am 不使用 相机 release in 表面被破坏 then 无法从另一个 Activity 再次启动 CameraActivity 简而言之 得到不幸的是应用程序已停止 错误 即使不释放相机 但如果我确实点击了 主页 按钮 来
  • 您可以传递“类型”作为参数吗?

    我想在 VB NET 中做类似以下的事情 可以吗 Function task value as Object toType as Type Return DirectCast value toType End Function 是的 有系统
  • codePointAt 和 charCodeAt 之间的区别

    有什么区别String prototype codePointAt and String prototype charCodeAt 在 JavaScript 中 A codePointAt 65 A charCodeAt 65 从 MDN
  • Google Games API 需要 games_lite 函数

    我使用 Google Play 游戏服务 我从官方示例中获取了代码 尝试使用 API 27 和 API 17 所有功能仅在一个帐户 所有者 Google 开发者控制台 下有效 在任何其他帐户下均有效 不行 我明白了 E AndroidRun
  • std::aligned_alloc() 的重新分配等效项是什么?

    我发觉到std aligned alloc https en cppreference com w cpp memory c aligned alloc进入 C 17 我喜欢它 但是 当我需要重新分配时会发生什么 我可以手动执行此操作 假设
  • 使用 jQuery live() 初始化插件?

    使用 jQuery 在特定类的所有当前和未来元素上自动初始化插件的最佳方法是什么 例如 假设我想要全部
  • 在 Python 中比较浮点数是否几乎相等的最佳方法是什么?

    众所周知 由于舍入和精度问题 比较浮点数是否相等有点繁琐 例如 比较浮点数 2012 年版 https randomascii wordpress com 2012 02 25 comparing floating point number
  • Dockerfile:通过 RUN 对文件系统所做的更改不会持久

    我找不到特定于网络搜索的内容site stackoverflow com dockerfile RUN fs changes not persisted I made Dockerfile并想通过以下方式对图像进行一些更改RUN 首先 我想
  • 如何使用 EMGU 计算 DFT 及其逆函数?

    如何计算图像的 DFT 使用 EMGU 显示它 然后计算反向值以返回原始图像 我将在这里回答我自己的问题 因为我花了一段时间才弄清楚 To test that it works here s an image and here s the
  • spring中如何进行单元测试验证注解

    我在类中有一些注释 例如 public class ProductModel Pattern regexp 1 1 9 0 9 message Quantity it should be number and greater than ze
  • Express 4 中的查询字符串变量仍然为空

    按照此处的建议编写简单的服务器堆栈溢出 https stackoverflow com questions 5710358 how to retrieve post query parameters 休息 api js const expr
  • 尝试使用 LoadImage 加载应用程序的图标,但该函数返回 0

    我正在尝试使用加载应用程序的图标LoadImageWinAPI函数 但由于某种原因它总是返回0 我已阅读文档 https msdn microsoft com en us library windows desktop ms648045 v
  • WINAPI/DWMAPI 不规则形状的模糊窗口

    注意 这不是关于无边框窗口的问题 因此 前几天我在 Windows 7 上浏览 开始 菜单时偶然发现了这个程序 它是一个本机 Windows 程序 称为 数学输入面板 现在 我对窗户的形状很好奇 我知道它并不是完全由 DWM 绘制的 因为边
  • 在另一个 Intent 中发送 Intent

    也许我的处理方式是错误的 但我想在我自己的应用程序中响应我的 Android AppWidget 的点击事件并启动一个 Activity 当时我设置了PendingIntent我还有一个Intent我想在点击时启动它 我的onStartCo
  • 如何对像 Excel Pivot 这样两个键必须匹配的数组求和?

    我尝试对 Datum 和 Material 必须匹配的所有 Menge 和 Fehler 值求和 结果应类似于 Excel 数据透视表 到目前为止 这是我的代码 但我不知道如何添加也必须匹配的第二个键 Material 我希望你能理解我试图