You need to enable JavaScript to run this app. 如何从指定结构对象数组中提取name、xp字段生成目标格式列表
优惠活动
大模型
产品
解决方案
定价
更多

如何从指定结构对象数组中提取name、xp字段生成目标格式列表

实现方案

核心逻辑是遍历数组提取对应字段拼接即可,以下是JavaScript的实现示例:

方案1:生成合并后的带换行的完整字符串

如果你需要把所有结果合并为一个字符串、逐行显示,可使用map+join组合:

// 你的原始数据数组
const originArr = [
  {
    _id: '6094d5746870c34a90d58d1b',
    userId: 'iduser1',
    name: 'User321',
    xp: 10,
    __v: 0
  },
  {
    _id: '6094d29ef94abe3ee4f144dc',
    userId: 'iduser',
    name: 'User123',
    xp: 5,
    __v: 0
  }
]

// 处理逻辑
const result = originArr.map(item => `${item.name}: ${item.xp}`).join('\n')

// 输出结果
console.log(result)

运行输出:

User321: 10
User123: 5

方案2:逐行直接输出

如果你只需要逐行打印结果,直接用forEach遍历即可:

originArr.forEach(item => {
  console.log(`${item.name}: ${item.xp}`)
})

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.22 03:06:08