【问题标题】:How to push to multi-dimensional array if value does not exist in javascript如果javascript中不存在值,如何推送到多维数组
【发布时间】:2021-01-27 10:16:28
【问题描述】:

我有一个多维数组,用于存储有关用户给出的问题和答案的信息,并依赖于 3 次检查。

  • 如果数组为空,则推送初始数据集
  • 如果数组中存在问题编号,则更新其相关答案
  • 如果不存在问题编号,则将问题信息和答案推送到数组中

我已成功完成前 2 次检查,但在进行第三次检查时遇到了问题。当我尝试推送新的问答信息时,它会推送多次重复而不是仅推送一个数据集。

我正在使用的功能是:

getBoolvalue(boolvalue, optionIndex, questionnNumber, option)
{
    if(this.answerboolvalues[0] == null)
    {
        this.answerboolvalues.push([questionnNumber, optionIndex, option, boolvalue]);
        console.log(this.answerboolvalues);
    
    }
    else
    {
        console.log("Array length : " + this.answerboolvalues.length);
        console.log("Array item : " + this.answerboolvalues[1][2]);
    
        for(let j = 0; j < this.answerboolvalues.length; j++)
        {
            if(this.answerboolvalues[j][0] == questionnNumber)  
            {
                if(this.answerboolvalues[j][1] == optionIndex)
                {
                    this.answerboolvalues[j][3] = boolvalue;
                    console.log(this.answerboolvalues);
                }
            }
            else if(this.answerboolvalues[j][0] != questionnNumber) 
            {
                this.answerboolvalues.push([questionnNumber, optionIndex, option, boolvalue]);
            }
        }
    }

}

【问题讨论】:

    标签: arrays typescript for-loop if-statement multidimensional-array


    【解决方案1】:

    使用.find 来查看该元素是否存在于数组中会更简洁。如果是,您可以根据需要对找到的项目进行变异,否则推送一个新数组。

    if(this.answerboolvalues[0] == null) 不需要检查,因为如果数组为空,则找不到元素,所以无论如何都会因为条件 3 被推送到数组中。

    还要修复questionNumber 的拼写错误,拼写错误很容易造成错误:

    getBoolvalue(boolValue, optionIndex, questionNumber, option) {
      const foundItem = this.answerboolvalues.find(
        subarr => subarr[0] === questionNumber && subarr[1] === optionIndex
      );
      if (foundItem) {
        foundItem[3] = boolValue;
      } else {
        this.answerboolvalues.push([questionNumber, optionIndex, option, boolValue]);
      }
    }
    

    但是这个数据结构很奇怪。考虑让answerboolvalues 成为一个由questionNumber 索引的对象,或者让它成为一个对象数组而不是数组数组:

    getBoolvalue(boolValue, optionIndex, questionNumber, option) {
      const foundItem = this.answerboolvalues.find(
        subarr => subarr.questionNumber === questionNumber && subarr.optionIndex === optionIndex
      );
      if (foundItem) {
        foundItem.boolValue = boolValue;
      } else {
        this.answerboolvalues.push({ questionnNumber, optionIndex, option, boolValue });
      }
    }
    

    【讨论】:

    • 嗨@CertainPerformance,感谢您的回复。该问题基本上遵循“选择以下哪些陈述是真/假”格式,其中多个陈述与用户选择的下拉列表相关联,这些陈述是真或假,因此问题编号是一个索引,然后另一个索引用于问题陈述匹配答案
    • 考虑一个由问题编号索引的数据结构,例如{ 2: [true, false, true, true] }对应于问题2,其中第一个选项为真,第二个为假,等等。这比当前更有意义接近
    • 我曾考虑过,但问题不是所有问题都是真/假,例如在 6 个问题中,问题 2 和问题 4 将是真假,因此不能使用问题编号作为索引
    • 然后为这些设置不同的数据格式,同时为这些设置一组真/假。
    猜你喜欢
    • 2019-08-09
    • 1970-01-01
    • 2011-12-14
    • 2019-12-07
    • 1970-01-01
    • 1970-01-01
    • 2015-07-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多