How do I update a value in a range variable?
I have a copied variable that stores an archive:
viewScope.MY_SCOPE = new Array();
viewScope.MY_SCOPE.push(["id0", 0, true]);
viewScope.MY_SCOPE.push(["id1", 1, false]);
viewScope.MY_SCOPE.push(["id2", 3, true]);
now I want to update one of the elements.
viewScope.MY_SCOPE[1][2] = "true";
and this is not with an error:
JavaScript action expression error is
put(int index,FBSValue value)
not supported inJavaWrapperObject
.
How can I update a specific element in an array?
source to share
When you add an SSJS array object to scope, it converts to java.util.Vector. Hence, if you want to set a value, you must use
viewScope.MY_SCOPE[1].set(2,"true");
instead of viewScope.MY_SCOPE[1][2] = "true";
.
I think the problem is that the use is ...[2] = "true"
trying to execute the method of the put
given object. While put
available in Maps such as HashMaps or area maps, vectors are used set
instead of to change values put
. For this reason, you receive the message "action expression put (...) not supported". In contrast, there is no problem to get the c variable viewScope.MY_SCOPE[1][2]
, because the method get
is available in both HashMaps and Vectors.
source to share
When storing arrays in scope variables, I would like to put the value in a properly typed javascript variable, make the changes, and then replace the scope variable with the updated value.
In your case, I would do the following:
var tempVar = viewScope.MY_SCOPE.toArray(); //convert to array to make sure properly typed
tempVar[1][2] = true;
viewScope.put(MY_SCOPE,tempVar);
Update: After testing the code along with mine, I also get the same error. To be honest, I would never have encountered multidimensional arrays in the first place. This is a great opportunity to use an array of objects:
var tempVar = []; // initialize the array
tempVar.push({val1:"id0",val2:0,val3:true});
tempVar.push({val1:"id1",val2:1,val3:false});
tempVar.push({val1:"id2",val2:3,val3:true});
viewScope.put("MY_SCOPE",tempVar);
Then, to change the desired value:
var tempVar = [];
tempVar = viewScope.get("MY_SCOPE");
tempVar[1].val3 = true;
viewScope.put("MY_SCOPE",tempVar)
I have tested this method and it works great.
source to share