问题
I intend to compare values between two views.
Imagine view1 has the following values:
Location | Officer
Australia| Peter
Beglium | John
Chile | Ben
Italy | Mike
Imagine view2 has the following values:
Item | Location
Book | Italy
Journal | Australia
Movie | Spain
What I would like to do is compare values between view1 and view2.
I write the following code:
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>");
}
}
When I run the program, I see view1[x] works fine as it can show values in view1, however about display the message, it only shows "value does not match" even the value exists in view1 and view2.
The result looks like the following:
Australia | value does not match
Beglium | value does not match
Chile | value does not match
Italy | value does not match
I would wonder to know why the message only display the else part and not sure why it does not show the proper message for each value in view1[x].
Actually I suppose the result will look this:
Australia | value matches
Beglium | value does not match
Chile | value does not match
Italy | value matches
What is the mistake in the code? Grateful for your advice please. Thank you.
回答1:
Change your code to
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>");
}
来源:https://stackoverflow.com/questions/38713626/xpages-compare-values-between-two-views