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

如何将TypeScript Record的所有值转换为单个数组?

如何将TypeScript的Record<string, number>转换为number[]数组?

最简单的方式是直接使用Object.values()方法,它会返回对象中所有值组成的数组,对于Record<string, number>类型,TypeScript可以自动推断出返回结果的类型为number[],无需额外处理:

// 示例Record对象
const data: Record<string, number> = {
  foo: 10,
  bar: 20,
  baz: 30
};

// 转换为number数组
const numberArray: number[] = Object.values(data);
// 结果: [10, 20, 30]

如果你原本考虑用flatMap,那通常是处理值本身为数组的场景(比如Record<string, number[]>),但在你的需求里,每个值都是单个数字,Object.values()已经是最直接高效的方案。

内容的提问来源于stack exchange,提问作者J. Hegg

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.06 02:01:06