You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在嵌套对象数组中高效查找所有CT属性的最大值?

从嵌套对象数组中获取最大CT值

给定如下结构的对象数组,需要提取所有工厂下所有产品的**CT(Cycle Time,周期时间)**的最大值,示例数据中的最大值为77:

const data = [{
    "202201": {
        "WO": 900,
        "WS": 0,
        "SY": 0.915,
        "LY": 0.98,
        "CT": 75
    },
    "202202": {
        "WO": 300,
        "WS": 0,
        "SY": 0.915,
        "LY": 0.98,
        "CT": 73
    },
    "202203": {
        "WO": 350,
        "WS": 0,
        "SY": 0.915,
        "LY": 0.98,
        "CT": 68
    },
    "202204": {
        "WO": 400,
        "WS": 0,
        "SY": 0.915,
        "LY": 0.98,
        "CT": 77
    },
    "202205": {
        "WO": 300,
        "WS": 0,
        "SY": 0.915,
        "LY": 0.98,
        "CT": 67
    },
    "Product": "A",
    "Facility": "a-Facility"
},
{
    "202201": {
        "WO": 6665,
        "WS": 0,
        "SY": 0.903,
        "LY": 0.993,
        "CT": 73
    },
    "202202": {
        "WO": 5907,
        "WS": 0,
        "SY": 0.903,
        "LY": 0.993,
        "CT": 71
    },
    "202203": {
        "WO": 5893,
        "WS": 0,
        "SY": 0.903,
        "LY": 0.993,
        "CT": 74
    },
    "202204": {
        "WO": 5486,
        "WS": 0,
        "SY": 0.903,
        "LY": 0.993,
        "CT": 67
    },
    "202205": {
        "WO": 5448,
        "WS": 0,
        "SY": 0.903,
        "LY": 0.993,
        "CT": 69
    },
    "Product": "B",
    "Facility": "b-Facility"
}]

已实现两种方案,但希望得到比双重循环更优雅的实现:

现有方案1

let numbers = [];
data.forEach(d => {
    numbers = numbers.concat(Object.keys(d).map(p => d[p].CT ? d[p].CT : 0));
})

const maxCycle = Math.max(...numbers);

现有方案2

let maxCycle = 0;
data.forEach(d => {
    for (const w in d) {
        maxCycle = maxCycle < d[w]['CT'] ? d[w]['CT'] : maxCycle;
    }
});

优雅实现方案

方案1:使用flatMap + Object.values + Math.max

利用扁平化API和值过滤,用链式调用实现简洁逻辑:

const maxCT = Math.max(
  ...data.flatMap(item => 
    Object.values(item)
      .filter(val => typeof val.CT === 'number')
      .map(val => val.CT)
  )
);
  • 逻辑说明:
    1. data.flatMap(...):遍历每个产品对象,将内部子对象数组扁平化
    2. Object.values(item):取出当前对象的所有值(排除键名)
    3. filter(...):筛选出包含有效CT数值的子对象(排除Product/Facility这类非目标属性)
    4. map(...):提取所有CT值,最后通过Math.max展开数组取最大值

方案2:使用reduce 减少中间数组开销

如果处理大数组,用reduce逐步计算最大值,避免生成额外数组:

const maxCT = data.reduce((globalMax, item) => {
  const currentItemMax = Math.max(
    ...Object.values(item)
      .filter(val => typeof val.CT === 'number')
      .map(val => val.CT)
  );
  return Math.max(globalMax, currentItemMax);
}, 0);
  • 逻辑说明:
    1. 外层reduce维护全局最大值,初始值设为0
    2. 对每个产品对象,先计算其内部的CT最大值
    3. 对比全局最大值,更新为更大的数值

TypeScript 类型安全版

针对TS项目,添加类型定义避免类型报错:

interface MonthData {
  WO: number;
  WS: number;
  SY: number;
  LY: number;
  CT: number;
}

interface ProductItem {
  [key: string]: MonthData | string;
  Product: string;
  Facility: string;
}

const data: ProductItem[] = [...]; // 你的数据

const maxCT = Math.max(
  ...data.flatMap(item => 
    Object.values(item)
      .filter(val => typeof (val as MonthData).CT === 'number')
      .map(val => (val as MonthData).CT)
  )
);

内容的提问来源于stack exchange,提问作者CSin84

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.19 13:15:31