如何在JavaScript中读取对象的空格分隔键

How to read space separated key of an object in JavaScript

本文关键字:空格 分隔 取对象 读取 JavaScript      更新时间:2023-09-26

帮助将是感激的,从一个对象中读取键,键与空格分隔

var Obj = {
    student class: '4th',
    student roll: '24'
}
console.log(obj.student class) ? throws error.
console.log(obj.student roll) ? throws error.

将其放入引号中并使用括号标记。

var Obj = {
  'student class': '4th',
  'student roll': '24'
};
console.log(Obj['student class']);
console.log(Obj['student roll']);

有两种方法可以读取对象

中的字段
  1. object.field
  2. 对象(领域)

要访问包含空格的字段,必须使用第二个选项

console.log (Obj("学生类"));console.log (Obj['学生滚']);

使用方括号:
object['property name']