JavaScript 如何获取 JSON 数组中的所有"名称"值?
javascriptweb developmentobject oriented programming更新于 2025/10/29 22:04:17
假设以下是我们的 JSON 数组减去
var details = [
{
"customerDetails": [
{
"customerName": "John Smith",
&"customerCountryName";: &"US";
}
]
},
{
&"customerDetails";: [
{
&"customerName";: &"David Miller";,
"customerCountryName": "AUS"
}
]
},
{
"customerDetails": [
{
"customerName": "Bob Taylor",
"customerCountryName": "UK"
}
]
}
]
若要仅获取 CustomerName 值,请使用 map() 的概念 −
示例
var details = [
{
"customerDetails": [
{
"customerName": "John Smith",
"customerCountryName": "US"
}
]
},
{
"customerDetails": [
{
"customerName": "David Miller",
"customerCountryName": "AUS"
}
]
},
{
"customerDetails": [
{
"customerName": "Bob Taylor",
"customerCountryName": "UK"
}
]
}
]
var allCustomerName = details.map(obj=>
obj.customerDetails[0].customerName);
console.log(allCustomerName);
要运行上述程序,您需要使用以下命令 −
node fileName.js.
这里的文件名是 demo206.js。
输出
PS C:\Users\Amit\javascript-code> node demo206.js [ 'John Smith', 'David Miller', 'Bob Taylor' ]
相关文章
有用资源
javascript 参考教程 - 该教程包含有关 javascript 的更多信息:https://www.cainiaomax.com/javascript/
打印
下一节:使用 JavaScript 在数组中查找总和为特定数字的两个数字的最佳方法是什么? ❯❮ 上一节:JavaScript 中的 addEventListener() 无法多次对按钮起作用?

