javascript - 在 javascript 中访问长链 JSON 的属性

标签 javascript node.js ecmascript-6

这似乎是一个奇怪的疑问,但很想知道。

在我的 vanilla javascript 项目中,必须访问对象内的不同属性。

考虑

{
students: {
 insideProperty1: {
  insideproperty2: {
   insideProperty3: {
    pro1: 1,
    pro2: 2,
    pro3: 3,
    .
    .
    .
    }
   }
  }
 }
}

我必须访问内部属性

我用吗

students.insideProperty1.insideProperty2.insideProperty3.pro1
students.insideProperty1.insideProperty2.insideProperty3.pro2
students.insideProperty1.insideProperty2.insideProperty3.pro3

const proVar = students.insideProperty1.insideProperty2.insideProperty3;
proVar.pro1
proVar.pro2
proVar.pro3

我问的是我的脚本将在生产中进行优化和压缩(使用 uglifyjs 和其他),所以我需要在开发中处理这个问题还是优化脚本会处理它?<​​/p>

最佳答案

你甚至可以通过数组解构来缩短。

const { pro1, pro2, pro3 } = students.insideProperty1.insideProperty2.insideProperty3;

关于javascript - 在 javascript 中访问长链 JSON 的属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55534329/

相关文章:

javascript将日期和时间转换为特定格式

javascript - 检测 Command + C 按键

javascript - 滑动开关 : + sign is not updated to -

javascript - React 组件未在 DOM 上渲染

javascript - 是否可以从其自己的处理程序对象中引用代理?

Javascript 过滤方程解释

javascript - IE8 : drag'n'drop files to a webpage

javascript - 是否可以在子模块的全局命名空间中保存要求(fs/http/等)?

node.js - 有没有办法检查当前导出是否已在 typescript 中导入?

javascript - 如何在 JavaScript ES6 类中链接异步方法