Home
Find the answer to your question
Why is my transform() function skipping the children variations? For example after the parent GROUP[0] , GROUP[3] is being processed instead of GROUP[1] and GROUP[2]. What's wrong with my tranform() function below?
function transform() {
LOGGER . log(" ");
LOGGER . log(" >>>> PRODUCT <<<<");
LOGGER . log("Group ID = " + getGroupColumnKey() + ", Group size = " + GROUP . length);
for(i=0;
i < GROUP . length;
i++) {
LOGGER . log(" ");
LOGGER . log("transformimg Group [" + i + "]");
PRODUCT_INRECORD = GROUP[i];
if (i == 0) {
LOGGER . log("--> transform parent: Group [" + i + "]");
transformParent();
}
LOGGER . log(" --> transforming child: Group [" + i + "]");
transformChild();
}
}
In the Product Transform Function(in the mapping-base.js)
The process log shows that the iteration variable "i" is being incremented randomly, more than +1, which means its being changed by functions out of scope.
In Java script its always a good habit to declare Variables as Local variables by declaring as -> var i = 0;
Var keyword declares the variable as a local variable. local to the fucntion its declared in, so that no external functions can change the variable.
The value of i is being incremented randomly in the transform() function, so the items are being processed ar 0, 3,4 etc instead of 0,1,2, etc.... So I removed For loop and replaced it with a while, so that i can declare "i" as a non local variable(Local variables hold their values only in the current scope, This prevents any external functions to increment/change the vars.).
So the final function would be
function transform() {
LOGGER.log(" ");
LOGGER.log(" >>>> PRODUCT <<<<");
LOGGER.log("Group ID = " + getGroupColumnKey() + ", Group size = " + GROUP.length);
/*for(i=0; i < GROUP.length; i++)
{
LOGGER.log(" ");
//LOGGER.log("transformimg Group [" + i + "]");
PRODUCT_INRECORD = GROUP[i];
if( i == 0)
{
//LOGGER.log("--> transform parent: Group [" + i + "]");
transformParent();
}else{
LOGGER.log(" --> transforming child: Group [" + i + "]");
transformChild();
}
}*/
for(var i=0; i < GROUP.length; i++)
{
LOGGER.log(" ");
//LOGGER.log("transformimg Group [" + i + "]");
PRODUCT_INRECORD = GROUP[i];
if( i == 0)
{
//LOGGER.log("--> transform parent: Group [" + i + "]");
transformParent();
}else{
LOGGER.log(" --> transforming child: Group [" + i + "]");
transformChild();
}
}
}