【问题标题】:target first array, first value in array of arrays目标第一个数组,数组数组中的第一个值
【发布时间】:2019-03-07 17:33:59
【问题描述】:

我有一个数组,如何定位第一个数组,第一个值?

questions: [
              [
                'Purchase only', 'Sale and purchase',
                'Sale only', 'Remortgage'
              ],
              [
                'Leasehold', 'Freehold'
              ],
              [
                'Yes', 'No'
              ]
            ]

所以我可以这样做:

    if (this.quoteResults[1] == this.questions[0][0]) 
DOESNT WORK

而不是这个:

    if (this.quoteResults[1] ===  'Purchase only') 
THIS WORKS BUT I WANT TO USE ARRAY INDEX INSTEAD

【问题讨论】:

  • console.log(this.questions[0][0]) 的作用是什么?
  • 您确定数据中有'Purchase only' 吗?也许它是小写的?然后尝试与小写的任何一方进行比较。或者,谁知道您可能只需要使用questions 而不是this.questions?不太确定您的演示代码。
  • 请提供minimal reproducible example。还描述“不起作用”是什么意思?运行代码时会发生什么?你想让它做什么不同的事情?

标签: javascript arrays vue.js


【解决方案1】:

当您使用“this”时,您的问题就会出现。您可以访问 this.quoteResults[1] 但无法访问“问题”。使用“this”时,请确保 quoteResults 和 questions 在您的范围内。尝试在 if 语句所在的函数内打印 questions[0][0] 。因为 questions[0][0] 是访问它的正确方法,所以这不是问题。 另外,我们不知道 quoteResults[1] 包含什么,它的数据类型是什么。

var questions = [
          [
            'Purchase only', 'Sale and purchase',
            'Sale only', 'Remortgage'
          ],
          [
            'Leasehold', 'Freehold'
          ],
          [
            'Yes', 'No'
          ]
        ]
      console.log(questions[0][0]);

【讨论】:

  • console.log(questions[0][0]) 有效,所以你是对的,谢谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-03-01
  • 2022-11-12
  • 2019-01-27
  • 1970-01-01
  • 1970-01-01
  • 2017-09-08
  • 1970-01-01
相关资源
最近更新 更多