• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

How to update an array of objects ?

Community Expert ,
Jun 17, 2016 Jun 17, 2016

Copy link to clipboard

Copied

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

TOPICS
Scripting

Views

379

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Jun 18, 2016 Jun 18, 2016

Thanks Apollo, but this did not do the trick.

But I have found that the order of definitions is crucial here. The following works as intended:

function oSeries (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
 

...

Votes

Translate

Translate
Explorer ,
Jun 17, 2016 Jun 17, 2016

Copy link to clipboard

Copied

Hi,

try in function oObjTpl "return this" in the end.

Regards

Apollo

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jun 18, 2016 Jun 18, 2016

Copy link to clipboard

Copied

LATEST

Thanks Apollo, but this did not do the trick.

But I have found that the order of definitions is crucial here. The following works as intended:

function oSeries (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
//return this;      // ahs no effect
}

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

var aSeries = [oSeries_0, oSeries_1, oSeries_2];   


var a1 = aSeries[2].Scheme;      // roman
$.writeln (a1);

// replace the zz by octogon
oSeries_0.Items[6] = "AAAA";   // this does not update aSeries
$.writeln (aSeries[0].Items);

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

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

/*
one,two,three,four,five,six,AAAA,seven,eight,nine,ten
one,two,three,four,five,six,BBBB,seven,eight,nine,ten
one,two,three,four,five,six,CCCC,seven,eight,nine,ten
*/

The order now is: set up the objects and then set up the array of objects.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines