我打算比较两个视图之间的值。
想象一下view1具有以下值:
Location | Officer
Australia| Peter
Beglium | John
Chile | Ben
Italy | Mike
想象一下view2具有以下值:
Item | Location
Book | Italy
Journal | Australia
Movie | Spain
我想要做的是比较view1和view2之间的值。
我写下面的代码:
var location = sessionScope.Location;
var message = "";
var view1 = @DbLookup(@DbName(), "view1", location , 0)); // use session scope variable to lookup in view1 and find location
var view2 = @DbLookup(@DbName(), "view2", view1 , 1)); // use view1 value to lookup in view
for(var x = 0; x < view1.length; x++)
{
for(var y = 0; y < view2.length; y++)
{
if(view1[x] == view2[y])//if value exist in view1 and view2
{
message = "value matches";
}
else // if not match
{
message = "value does not match";
}
// show the result in the excel
//first row list all values in view1 and the second row will show the message
writer.write("<tr><td>" + view1[x] + "</td></tr>" + message + "</td></tr>");
}
}
当我运行程序时,我看到view1 [x]工作正常,因为它可以在view1中显示值,但是关于显示消息,它只显示“值不匹配”,即使view1和view2中存在该值。 / p>
结果如下所示:
Australia | value does not match
Beglium | value does not match
Chile | value does not match
Italy | value does not match
我想知道为什么消息只显示else部分而不确定为什么它没有为view1 [x]中的每个值显示正确的消息。
实际上我认为结果会是这样的:
Australia | value matches
Beglium | value does not match
Chile | value does not match
Italy | value matches
代码中的错误是什么?感谢您的建议。谢谢。
答案 0 :(得分:0)
将您的代码更改为
for(var x = 0; x < view1.length; x++) {
message = "value does not match";
for(var y = 0; y < view2.length; y++) {
if(view1[x] == view2[y]) {
message = "value matches";
break;
}
}
// show the result in the excel
//first row list all values in view1 and the second row will show the message
writer.write("<tr><td>" + view1[x] + "</td></tr>" + message + "</td></tr>");
}