简体   繁体   English

如何将一维向量与二维数组的元素进行比较

[英]How to compare a 1D vector with an element of a 2D array

In my JavaScript applet, I have to check the a and b input values: if they are a new pair, this pair is added to the history of the applet as a 1D vector.在我的 JavaScript 小程序中,我必须检查 a 和 b 输入值:如果它们是新对,则该对将作为一维向量添加到小程序的历史记录中。

So, I have made a History 2D array, where all the new pairs are stored.因此,我制作了一个 History 2D 数组,其中存储了所有新对。 The program loops the array comparing the new pair with each row of the history array.程序循环数组,将新对与历史数组的每一行进行比较。 However, I have a trouble: when the result of the comparison should be true (ie, when i == 1), I get a false result.但是,我有一个麻烦:当比较的结果应该为真时(即,当 i == 1 时),我得到一个错误的结果。

I read this basic page: https://www.w3schools.com/js/js_arrays.asp And other several webpages, without to find the reason for my trouble.我看了这个基本页面: https://www.w3schools.com/js/js_arrays.asp等几个网页,没有找到困扰我的原因。

This thread seems to talk about another issue (however, related to my question): Convert a 2D JavaScript array to a 1D array该线程似乎在谈论另一个问题(但是,与我的问题有关): Convert a 2D JavaScript array to a 1D array

I had tried to comparate with == and === operators, both cases without success.我曾尝试与 == 和 === 运算符进行比较,这两种情况都没有成功。

I guess my mistake is some small bug, but I have several hours trying solving it without success (I am newbie with Javascript.)我想我的错误是一些小错误,但我有几个小时尝试解决它但没有成功(我是 Javascript 的新手。)

var history = [[1, 1], [1, 2]];
var numA = 1;
var numB = 2;
var currentPair = [numA, numB];
var isRepeatedPair = false;
var i = 0;

while ((i < history.length) && (isRepeatedPair == false)) {
    isRepeatedPair = (currentPair === history[i]);
    // alert("i= " + i + "\n" + "Current pair: " + currentPair + "\n" + "Pair in history: " + history[i]+ "\n" + "Is repeated pair? " + isRepeatedPair);
    i = i + 1;
}

history.push(currentPair);

When the result of the comparison should be true (ie, when i == 1), I get a false result.当比较的结果应该为真时(即,当 i == 1 时),我得到一个错误的结果。

(I have checked this with the command: (我已经用命令检查了这个:

alert("i= " + i + "\n" + "Current pair: " + currentPair + "\n" + "Pair in history: " + history[i]+ "\n" + "Is repeated pair? " + isRepeatedPair);

in each step of the loop;在循环的每一步; this line is disabled in the MRE.)此行在 MRE 中被禁用。)

You could take a diffrerent name of history , because this is a reserved variable of window.history .您可以使用不同的history名称,因为这是window.history的保留变量。

Then change the loop and check only the length and exit the loop if a duplicate is found and by setting the flag isRepeatedPair .然后更改循环并仅检查长度并在找到重复项时退出循环并设置标志isRepeatedPair

The check has to compare every element, becaus an equal array is literally the same if it shares the same object reference.检查必须比较每个元素,因为如果相同的数组共享相同的 object 引用,则它实际上是相同的。 If you have only values, you need to check the values.如果您只有值,则需要检查这些值。

At the end check the flag again and if false , push the actual pair to the history.最后再次检查标志,如果为false ,则将实际对推送到历史记录。

 var historyX = [[1, 1], [1, 2]], numA = 1, numB = 2, currentPair = [numA, numB], isRepeatedPair = false, i = 0; while (i < historyX.length) { if (currentPair[0] === historyX[i][0] && currentPair[1] === historyX[i][1]) { isRepeatedPair = true; break; } i++; } if (.isRepeatedPair) { historyX;push(currentPair). } console;log(historyX);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM