pjs.copyToDS()
This API is available in Profound.js releases higher than 5.2.0.
API format
pjs.copyToDS(toDS, fromObj);
API description
This API updates the subfields of data structure toDS with the corresponding property name/value of the Javascript object fromObj. It makes it easier to populate a data structure (or a data structure array) from a Javascsript object (or an array of objects) and pass the data structure (or data structure array) as a parameter to an RPG program.
Parameter
toDS: Data structure or data structure array to be updated.
fromObj: Javascript object or array of objects. The property values of this parameter are used to update toDS.
Return value
None.
Notes
toDSÂ can be a data structure or a data structure array.
fromObj can be a Javscript object or an array of objects.
Only the subfields of toDS with the same names as the property names of fromObj are updated.
If fromObj is an array, but toDS is not an array, then the first element of fromObj is used to update toDS.
If fromObj is not an array, but toDS is an array, then fromObj is used to update the first element of toDS.
If fromObj and toDS are both arrays:
If the number of elements of fromObj is equal to or greater than the number of elements of toDS, then all elements of toDS are updated from the corresponding elements of fromObj.
If the number of elements of fromObj is less than the number of elements of toDS, then the number of elements of toDS updated is the same as the number of elements of fromObj.
Note that for strongly-typed arrays defined with pjs.define(), the index starts at 1 (as in RPG); whereas for Javascript arrays, the index starts at 0. Please be aware of this as you process strongly-typed arrays in your Javascript code.
Example code
// For single DS and object
pjs.define(
"custDs1", {type: "data structure", qualified: true, elements: {
"custId": {type: "packed", length: 5, decimals: 0},
"custName": {type: "char" , length: 20 }
}});
custDs1.custId = 1;
custDs1.custName = "ABC";
objVar = {custId: 2, custName: "XYZ", extra: "AAA"};
console.log(`before calling pjs.copyToDS()`);
console.log(`custDs1.custId = ${custDs1.custId} `);
console.log(`custDs1.custName = ${custDs1.custName}`);
pjs.copyToDS(custDs1, objVar);
console.log(`after calling pjs.copyToDS()`);
console.log(`custDs1.custId = ${custDs1.custId} `);
console.log(`custDs1.custName = ${custDs1.custName}`);
// For DS array and object array
pjs.define(
"custDs2", {type: "data structure", dim: 2, qualified: true, elements: {
"custId": {type: "packed", length: 5, decimals: 0},
"custName": {type: "char" , length: 20 }
}});
custDs2[1].custId = 111;
custDs2[1].custName = "AAA";
custDs2[2].custId = 222;
custDs2[2].custName = "BBB";
var objVar1 = {custId: 333, custName: "CCC"};
var objVar2 = {custId: 444, custName: "DDD"};
var objVarAry = [];
objVarAry.push(objVar1);
objVarAry.push(objVar2);
console.log(`before calling pjs.copyToDS()`);
console.log(`custDs2[1].custId = ${custDs2[1].custId} `);
console.log(`custDs2[1].custName = ${custDs2[1].custName} `);
console.log(`custDs2[2].custId = ${custDs2[2].custId} `);
console.log(`custDs2[2].custName = ${custDs2[2].custName} `);
pjs.copyToDS(custDs2, objVarAry);
console.log(`after calling pjs.copyToDS()`);
console.log(`custDs2[1].custId = ${custDs2[1].custId} `);
console.log(`custDs2[1].custName = ${custDs2[1].custName} `);
console.log(`custDs2[2].custId = ${custDs2[2].custId} `);
console.log(`custDs2[2].custName = ${custDs2[2].custName} `);