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

如何使用Node.js提取JSON数组中指定字段值并生成目标数组

解决代码

直接使用数组的map方法遍历提取name字段即可:

const jsondata = [{ "name": "StorageType", "value": "AllStorageTypes" }, { "name": "BucketName", "value": "testing" }]
const res = jsondata.map(item => item.name)
console.log(res)

运行后输出结果为 ["StorageType", "BucketName"],变量res可直接后续使用。
如果需要兼容不支持箭头函数的环境,可改写为:

const res = jsondata.map(function(item) {
  return item.name
})

原代码问题说明

你使用Object.keys处理数组得到的是数组的索引值,遍历后取到的是每个完整的对象元素,没有提取对象内的name字段,且forEach没有返回值,无法直接生成目标数组。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.25 01:45:03