【问题标题】:Why is the length of nested array returning as undefined even after I've just pushed a new array?为什么即使我刚刚推送了一个新数组,嵌套数组的长度仍返回未定义?
【发布时间】:2021-09-03 14:29:11
【问题描述】:

我已经开发 Slack 应用程序有一段时间了。我有一个按钮,它根据个人用户 ID 和名称创建提交 ID,该按钮将与从未来模式输入中收集的一些其他信息一起被推送到一个数组中。然后将该数组推入另一个数据库数组以跟踪所有提交数组。

我正在尝试构建一个函数来遍历嵌套数组并检查参数(submissionId)是否已经在嵌套数组中。

当我运行我的代码时,它返回“TypeError: Cannot read property 'length' of undefined。”

在我的代码中,您会看到我在检查 if 语句之前将一个新的子数组推送到我的父数组。为什么它会返回未定义?确实有一个子数组可以检查长度。

  // Function to check if submission ID already exists in DB
  function checkDupe(submissionId) {
  
  // Result to track if ID is found or not, view to return the correct view based on result
  let result;
  let view;

  // Push new array for the sake of testing; should always return true in this case
  payrollDb.push(new Array(submissionId));

  // This logs correctly with the submissionId param
  console.log(payrollDb[0][0]);

  // Nested for-loop to iterate through subarray values and check for submissionId
  for(let i=0; i<payrollDb.length; i++) {
    for(let j=0; j<payrollDb[i].length; j++) {
       console.log(submissionId,"\n", payrollDb[i][j]);
      payrollDb[i][j] === submissionId ? result = true : result = false;
    }
    
  };

  // If found, return the error modal, if not continue with the next modal.
  result ? view = views_payroll_duplicate : view = views_payroll_period;

  return view;
  
}

我在这里不知所措。我只是希望它检查 submitId 是否已存在于我的“数据库”中,如果存在,则返回一个 JSON 正文,如果找不到重复则返回另一个 JSON 正文。

任何关于如何修复甚至改进我所拥有的建议都将不胜感激!

【问题讨论】:

  • 还没有看完整个代码,但是我注意到你没有跳出循环,以防找到submitId,这意味着下一次迭代结果可以再次设置为false。还有result ? view = views_payroll_duplicate : view = views_payroll_period; 你是不是打算用view = result ? views_payroll_duplicate : views_payroll_period 来做实际的分配?
  • payrollDb 可能有一个不是数组的值,看不到它的来源,提供足够的可重现代码
  • 看起来您希望 new Array(n) 返回 [n],但实际上它返回 [empty x n]
  • 由于payrollDb 似乎在您的函数之外被实例化(并且可能被修改),我们无法知道它包含什么。要么,您首先检查可以读取长度属性,要么确保 payrollDb 仅包含预期值。

标签: javascript node.js arrays slack slack-api


【解决方案1】:

我敢打赌,其中一个 payrollDb[i] 是未定义的。在这行之后:

for(let i=0; i&lt;payrollDb.length; i++) {

在下一个循环之前添加一个检查以记录 [i]。然后,当它出错时,应该会显示导致问题的位置,并且您应该能够围绕它进行编码。如果它未定义,您还可以将第二个 for 包装在条件中以跳过它。

  for(let i=0; i<payrollDb.length; i++) {
      if ([i] === undefined) { // check the type equals undefined
          for(let j=0; j<payrollDb[i].length; j++) {

【讨论】:

  • 我在第一个 for 循环之后添加了检查,它正确地注销了我之前推送的数据。我还添加了一个 if 来停止,如果它未定义,但它会继续,然后仍然说未定义。就好像一切都很好,直到我检查长度然后它就像??? DNE,兄弟。
  • 量子阵列,其中结果在测量时会发生变化。哈哈哈。
【解决方案2】:

好吧,我似乎找到了问题所在。老实说,我不知道为什么这会奏效,但确实奏效了。

我在循环之前添加了一个变量,该变量等于我要循环遍历的数组的长度,然后在我的 for 循环中使用它,而不是直接调用 payrollDb.length 或 payrollDb[i].length。

更新后的代码如下。

function checkDupe(submissionId) {

  let result;
  let view;

  // Set variable for outer array length
  let n = payrollDb.length;
  for(let i=0; i<n; i++) {

    // Set variable for inner array length
    let m = payrollDb[i].length;
    for(let j=0; j<m; j++) {
      payrollDb[i][j] === submissionId ? result = true : result = false;
    }
  };

  result ? view = views_payroll_duplicate : view = views_payroll_period;

  return view;
  
}

如果有人能进一步解释,那就太好了!!!

【讨论】:

    猜你喜欢
    • 2019-10-31
    • 2021-09-28
    • 1970-01-01
    • 1970-01-01
    • 2019-04-09
    • 1970-01-01
    • 2023-03-26
    • 2016-09-01
    • 1970-01-01
    相关资源
    最近更新 更多