Quantcast
Channel: Adobe Community : All Content - FrameMaker Scripting
Viewing all articles
Browse latest Browse all 888

How to update an array of objects ?

$
0
0

Dear friends,

I have a number of objects stored in an array.
It seems that the array does not point to the objects, but has copies of them.
The item in the array is only updated with array[index].prop notation, not with the object.prop notation.

It would be possible to always use the array[index].prop notation for an update of items, but I want to understand, why the other method does not work. And of course there should be no copies...

 

var objArray = [oObject0, oObject1, oObject2];   

function oObjTpl (name, scheme, start, incr, current, decimals, items) { // 'prototype' definition
  this.Name =       name;  this.Scheme =     scheme;                       // numermic | roman | ROMAN | text  this.Start =      start;                        // start value  this.Incr =       incr;                         // increment, may be a formula string  this.Current =    current;                      // Last inserted value or item  this.Decimals =   decimals;                     // relevant only for numeric scheme  this.Items =      items;                        // array of list items, relevant only for text scheme
}

var items0 = ["one","two", "three", "four", "five", "six", "zz", "seven", "eight", "nine", "ten"];
var oObject0 = new oObjTpl ('CountingEN', 'text',     1,  1, undefined, undefined, items0);
var oObject1 = new oObjTpl ('oObject1',  'numeric', 17, -1, undefined,         0, undefined);
var oObject2 = new oObjTpl ('oObject2',  'roman',    1,  1, undefined,         0, undefined);

// replace the zz by soemething else
oObject0.Items[6] = "AAAA";       // this does not update objArray
$.writeln (objArray[0].Items);

objArray[0].Items[6] = "BBBB";    // this updates objArray
$.writeln (objArray[0].Items);

oObject0.Items[6] = "CCCC";       // update of objArray effective in next run
$.writeln (objArray[0].Items);

 

When running the script 3 times I get this console output:

one,two,three,four,five,six,zz,seven,eight,nine,ten
one,two,three,four,five,six,BBBB,seven,eight,nine,ten
one,two,three,four,five,six,BBBB,seven,eight,nine,ten
Result: undefined
one,two,three,four,five,six,CCCC,seven,eight,nine,ten
one,two,three,four,five,six,BBBB,seven,eight,nine,ten
one,two,three,four,five,six,BBBB,seven,eight,nine,ten
Result: undefined
one,two,three,four,five,six,CCCC,seven,eight,nine,ten
one,two,three,four,five,six,BBBB,seven,eight,nine,ten
one,two,three,four,five,six,BBBB,seven,eight,nine,ten
Result: undefined

Viewing all articles
Browse latest Browse all 888

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>