在for循环中分配变量

assign variable in a for-loop

我正在尝试将计数最低的元素分配到新数组 属性 lowestAlph 并给我下面的迭代,其中 -2 被存储,而不是简单地输出最低计数 -2 本身:

[["n"], ["o", "p"], ["q"], ["r", "s"], -2]

但是,它没有使用 push 方法打印正确的输出。 有人可以帮忙吗?

const example = {
    alph: [
        [["a"], ["b", "c"], ["d"], ["e", "f"], 23],
        [["g"], ["h", "j"], ["k"], ["l", "m"], 19],
        [["n"], ["o", "p"], ["q"], ["r", "s"], -2],
        [["t"], ["u", "w"], ["x"], ["y", "z"], 25]
    ],

    lowestAlph: [],


    calcLow: function () {
        let lowest = this.alph[0][4];
        for (let i = 0; i < this.alph.length; i++) {
            for (let n = i + 1; n < this.alph.length; n++) {
                if (this.alph[n][4] < lowest)
                    lowest = this.alph[n][4]
            }
                //this.lowestAlph.push(this.alph[i])
        }
        return lowest;
    }
}

console.log(example.calcLow());

您目前只检索数组 this.alph[n][4] 中的最后一个元素并将其分配给 lowest 变量。

相反,您应该使用 this.alph[n] 将整个数组分配给 lowest,然后在进行比较时检索最后一个元素。

请参阅下面的示例。

const example = {
    alph: [
        [["a"], ["b", "c"], ["d"], ["e", "f"], 23],
        [["g"], ["h", "j"], ["k"], ["l", "m"], 19],
        [["n"], ["o", "p"], ["q"], ["r", "s"], -2],
        [["t"], ["u", "w"], ["x"], ["y", "z"], 25]
    ],

    lowestAlph: [],


    calcLow: function () {
        let lowest = this.alph[0];
        for (let i = 0; i < this.alph.length; i++) {
            for (let n = i + 1; n < this.alph.length; n++) {
                if (this.alph[n][4] < lowest[4])
                    lowest = this.alph[n]
            }
        }
        return lowest;
    }
}

console.log(example.calcLow());

如果以下正确,它将 return 预期输出:

  1. 去除嵌套循环(一个for循环可以找出最小值,因为你的目的不是对alph进行排序)

  2. if found lower value, uses = operator instead of Array.push because it will keep adding new value into your lowestAlph if lower value if found

  3. return this.lowestAlph 而不是 lowest

const example = {
    alph: [
        [["a"], ["b", "c"], ["d"], ["e", "f"], 23],
        [["g"], ["h", "j"], ["k"], ["l", "m"], 19],
        [["n"], ["o", "p"], ["q"], ["r", "s"], -2],
        [["t"], ["u", "w"], ["x"], ["y", "z"], 25]
    ],

    lowestAlph: [],


    calcLow: function () {
        let lowest = this.alph[0][4];
        for (let i = 0; i < this.alph.length; i++) {
            // for (let n = i + 1; n < this.alph.length; n++) { // remove this for-loop
                if (this.alph[i][4] < lowest)
                    this.lowestAlph = this.alph[i] // assign with the array instead of Array.push
            // }
                //this.lowestAlph.push(this.alph[i])
        }
        return this.lowestAlph; // return this.lowestAlph instead of lowest
    }
}

console.log(example.calcLow());

您可以使用展开运算符

const example = {
    alph: [
        [["a"], ["b", "c"], ["d"], ["e", "f"], 23],
        [["g"], ["h", "j"], ["k"], ["l", "m"], 19],
        [["n"], ["o", "p"], ["q"], ["r", "s"], -2],
        [["t"], ["u", "w"], ["x"], ["y", "z"], 25]
    ],

    lowestAlph: [],


    calcLow: function () {
        let lowest = this.alph[0][4];
        for (let i = 0; i < this.alph.length; i++) {
            for (let n = i + 1; n < this.alph.length; n++) {
                if (this.alph[n][4] < lowest)
                    lowest = this.alph[n][4]
            }
           this.alph[i].includes(lowest) ?
                this.lowestAlph = [...this.alph[i]] : null
        }
        return this.lowestAlph
    }
    
}

console.log(example.calcLow());

一个简单的解决方案是根据第 4 个索引值和 return 最低的索引值对数组进行克隆和排序。例如

const alpha = [
  [["a"], ["b", "c"], ["d"], ["e", "f"], 23],
  [["g"], ["h", "j"], ["k"], ["l", "m"], 19],
  [["n"], ["o", "p"], ["q"], ["r", "s"], -2],
  [["t"], ["u", "w"], ["x"], ["y", "z"], 25]
]

const sorted = [...alpha].sort((a, b) => b[4] - a[4])
console.log(JSON.stringify(sorted.pop()))