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

JavaScript新手求教:如何获取数组内对象的'student-name'属性值?

获取数组对象中指定值的方法

你的数组里仅包含一个对象元素,要拿到"Harry Potter"可以按以下方式操作:

直接获取写法

通过数组索引定位到对象,再用方括号访问带连字符的属性:

var arr = [{"student-name":"Harry Potter","discipline":"Magic"}];
var studentName = arr[0]["student-name"];
console.log(studentName); // 输出 Harry Potter

分步拆解(更适合新手理解)

  1. 先取出数组里的第一个对象:
var studentObj = arr[0]; // 此时studentObj就是{"student-name":"Harry Potter","discipline":"Magic"}
  1. 再从对象中提取目标值:
var studentName = studentObj["student-name"];

注意:因为属性名student-name包含连字符,不能使用点语法(比如studentObj.student-name),这类带特殊字符的属性必须用方括号包裹属性名来访问。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.07 21:01:11