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

  1. toDS: Data structure or data structure array to be updated.

  2. fromObj: Javascript object or array of objects. The property values of this parameter are used to update toDS.

Return value

None.

Notes

  1. toDSĀ can be a data structure or a data structure array.

  2. fromObj can be a Javscript object or an array of objects.

  3. Only the subfields of toDS with the same names as the property names of fromObj are updated.

  4. If fromObj is an array, but toDS is not an array, then the first element of fromObj is used to update toDS.

  5. If fromObj is not an array, but toDS is an array, then fromObj is used to update the first element of toDS.

  6. If fromObj and toDS are both arrays:

    1. 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.

    2. 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.

  7. 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} `);