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

如何访问键名含@符号的对象属性?解决Unexpected character '@'错误

如何访问键名包含@符号的对象属性值

当对象的键名包含@这类特殊字符时,**不能使用点语法(.)**访问属性——因为点语法要求键名必须是合法的标识符(不能以特殊字符开头),这就是你遇到Unexpected character '@'错误的原因。

正确的做法是使用方括号语法([]),把键名作为字符串放在方括号里:

假设你的对象变量名为product:

const product = {
  id: 'v6y4fra9dd3xxys',
  created: '2022-10-29 08:48:12.585',
  updated: '2022-11-14 08:03:46.477',
  '@collectionId': 'x16irtg922kc0cg',
  '@collectionName': 'products',
  condition: 'new',
  image: 'short4.jpg',
  name: 'Short 4',
  price: 19000,
  productImg: 'short4_iKD5NIhZWX.jpg',
  quantity: 1,
  type: 'shortboard',
  '@expand': {}
};

要获取@collectionName的值,直接用:

const collectionName = product['@collectionName'];
console.log(collectionName); // 输出:products

同理,其他带@的键也可以用同样方式访问,比如@collectionId或@expand:

const collectionId = product['@collectionId'];
const expandData = product['@expand'];

方括号语法支持所有合法的字符串键名,不管是否包含特殊字符,是处理这类场景的标准方案。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.12 12:20:54