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

如何使用TypeScript将Map键值对转为对象存入数组第一个位置

实现代码

你可以直接使用Object.fromEntries完成转换,这是最简洁的写法:

const targetArray = [Object.fromEntries(this.mapValues)]

执行后targetArray就是你需要的[{1: "First", 2: "Second", 3: "Third"}]格式。

兼容旧环境的写法

如果你的运行环境不支持ES2019的Object.fromEntries,可以用遍历Map的方式手动构建:

const tempObj = {}
this.mapValues.forEach((value, key) => {
  tempObj[key] = value
})
const targetArray = [tempObj]

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.02 18:09:02