【问题标题】:How to print object notation from variable如何从变量打印对象符号
【发布时间】:2018-06-03 20:22:21
【问题描述】:

我有一个关于使用带有对象表示法的变量的问题。假设我有以下代码:

let anObject = {
    name: 'Joaquin',
    age: 27,
    occupation: 'software developer',
    interests: ['walking', 'Arduino', 'working']
}
let print = 'interests[0]';
console.log(anObject.interests[0]); //prints the result
console.log(anObject.print); //prints undefined

如果我登录anObject.interests[0],它会打印出预期的结果。但是当我将interests[0] 存储在变量中并再次打印时,它不会。

我们如何克服这个问题,为什么它没有打印出预期的结果?

【问题讨论】:

  • print 是字符串“interest[0]”,它不检索变量。尝试不带引号的兴趣[0]。
  • @MosèRaguzzini: Uncaught ReferenceError: interest is not defined
  • 显然:anObject.interests[0]

标签: javascript variables object printing


【解决方案1】:

这不是 JavaScript 对象访问的工作原理。

anObject.print 正在寻找 print 对象上的 print 属性。
它与您的print 变量无关

JavaScript 原生支持使用字符串访问对象的唯一方法是使用单独的属性键,例如:

console.log(anObject['interests'][0]);

【讨论】:

    【解决方案2】:

    在 javascript 中,我们可以通过以下方式访问: - 点符号 - 方括号

    但只有第二种情况允许动态访问属性,但您不能直接指定索引为interests[0] 的键。您必须传递返回数组对象的键,然后您将访问您想要获取的特定索引:

    let anObject = {
        name: 'Joaquin',
        age: 27,
        occupation: 'software developer',
        interests: ['walking', 'Arduino', 'working']
    }
    let print = 'interests';
    let i = 0;
    let print2 = 'age'
    console.log(anObject.interests[0]); //prints the result
    console.log(anObject[print]); // prints array object
    console.log(anObject[print][i]); // print first value of array object
    console.log(anObject[print2])

    【讨论】:

      【解决方案3】:

      每当在 javascript 对象中访问属性时,它都会被字符串化。 Javascript 将 anObject.print 评估为 anObject 中不存在的属性,因此打印未定义。要获得所需的结果,请进行以下更正。

          let anObject = {
              name: 'Joaquin',
              age: 27,
              occupation: 'software developer',
              interests: ['walking', 'Arduino', 'working']
          }
          let print = 'interests';
          console.log(anObject.interests[0]); //prints the result
          console.log(anObject[print][0]); //prints the result i.e. walking
      

      【讨论】:

        【解决方案4】:

        以下是一些示例,可帮助您更好地了解正在发生的事情以及如何使其发挥作用:

        let anObject = {
            name: 'Joaquin',
            age: 27,
            occupation: 'software developer',
            interests: ['walking', 'Arduino', 'working'],
            'interests[0]': 'Hello SO !'
        }
        
        // As expected
        console.log(anObject.interests[0]); //prints 'walking'
        
        // Here you try to access the property named 'interests[0]'
        let print = 'interests[0]';
        console.log(anObject[print]); //prints 'Hello SO !'
        
        // You can use eval and passing the entire expression to evaluate (Not recommended)
        console.log(eval('anObject.' + 'interests[1]'));
        
        // Better solution using Lodash
        console.log(_.get(anObject, 'interests[1]'));
        <script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.js"></script>

        【讨论】:

        • 当然,eval“有效”,但不要永远建议将其作为有效的解决方案。并且将库用于像对象访问这样基本的东西并不是“更好”。
        • 已添加 不推荐。根据 OP 的问题,它可能是唯一的解决方案
        • 这不是唯一的解决方案。为这些字符串编写适当的解析器相对简单。
        • 你想说什么?
        • 问题是如何根据可能是'interests[0]' 的字符串访问属性。不是如何直接访问该属性。对我来说,编写“这些字符串的解析器”也不是“相对微不足道的”。
        【解决方案5】:

        您不能仅使用本机 JavaScript 来执行此操作。唯一有效的情况是您的属性字符串仅包含一个道具,而不是prop[0]。并且不能通过点符号来读取道具。你必须使用:

        obj[propString]
        

        lib lodash 有函数_.get(),如果你需要它可以做到这一点!

        【讨论】:

          【解决方案6】:
          anObject
          

          没有附加到对象的打印键。您正在为与对象无关的变量分配一个简单的字符串。

          一个可能的解决方案是:

          let print = anObject.interests[0]
          console.log(print)
          

          我不知道你在做什么,所以请详细说明,以便我提供更多帮助。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2021-12-13
            • 1970-01-01
            • 1970-01-01
            • 2015-02-08
            • 1970-01-01
            • 2015-09-03
            • 2015-03-01
            • 1970-01-01
            相关资源
            最近更新 更多