Skip to main content
K.Daube
Community Expert
Community Expert
July 4, 2016
Answered

Updating a global array

  • July 4, 2016
  • 1 reply
  • 529 views

Dear friends!

I do not remmber what I have done some days ago - global arrays are no more updated. I have cut it down to this mechanism:

var arrayOfObjects = [];             // global Array

UpdateA (arrayOfObjects);            // array is NOT updated
$.writeln ("1) arraOfObjects = \n" + arrayOfObjects);

UpdateB (arrayOfObjects);            // array is NOT updated either
$.writeln ("2) arraOfObjects = \n" + arrayOfObjects);

function UpdateA (gArray, someotherStuff) {
var locObject = new Object();
    locObject.Name = "A";
    locObject.Item = "Something";

    gArray = [locObject, locObject, locObject];
$.writeln ("A) gArray = " + gArray);
}

function UpdateB (gArray, someotherStuff) {
var locObject = new Object();
    locObject.Name = "A";
    locObject.Item = "Something";
 
var localArray = [locObject, locObject, locObject];
    gArray = localArray.slice();
$.writeln ("B) gArray = " + gArray);
}

The output is quite clear: the gobal array is not touched at all by the Update functions:

A) gArray = [object Object],[object Object],[object Object]
1) arraOfObjects =

B) gArray = [object Object],[object Object],[object Object]
2) arraOfObjects =

Have I become blind?

In the original script the update function collects markers and (did so) put them into the global array.

It seems that I need a tool to save changes ...

Klaus

This topic has been closed for replies.
Correct answer K.Daube

Dear friends,

After much fiddling around I have found solutions to the desired array functions. One essential hint was that

gArray = [];

wihin a function creates a new array, not emptying the original one.

So Now I have a set of functions for arrays. It may be trivial for You, but not for me...

gArray = [];
gArray2 = ["1", "2", "3", "4", "5"];                               

AmendArray (gArray, "one", "two", "three", "four", "five");          //1 gArray =                
$.writeln ("1 gArray = \n   " + gArray);                             //  one,two,three,four,five  OK

DeleteArrayItem (gArray, 1);                       // remove item 2  //2 gArray =                
$.writeln ("2 gArray = \n   " + gArray);                             //  one,three,four,five      OK  

CopyArray (gArray, gArray2);                                         //3 gArray =                
$.writeln ("3 gArray = \n   " + gArray);                             //  1,2,3,4,5                OK

InsertArrayItem (gArray, 1, "two");                                  //4 gArray =                
$.writeln ("4 gArray = \n   " + gArray);                             //  1,two,2,3,4,5            OK

ReplaceArrayItem (gArray, 3, "three");                               //5 gArray =                
$.writeln ("5 gArray = \n   " + gArray);                             //  1,two,2,three,4,5        OK

ExtractArray (gArray, gArray2, 2, 3);          // extract items 3..4 //6 gArray, gArray2          OK 
$.writeln ("6 gArray, gArray2 = \n   " + gArray + "\n   " + gArray2);//  3,4,5
                                                                     //  1,2                    
function AmendArray (array, items) {
  var j, n = arguments.length;
  for (j = 1;  j < n; j++) {
    array.push(arguments);
  }
}

function DeleteArrayItem (array, index) {
// array.splice(index, n-delete,insert-item1,.....,itemX)
  array.splice (index, 1);
}

function CopyArray (target, source) {
// http://www.2ality.com/2012/12/clear-array.html tells:
// target = [];        Creates extra garbage: existing array not reused, a new one is created.
// target.length = 0;  JavaScript semantics dictate that all elements must be deleted; costs time
  var j, n = source.length;
  target.length = 0;      
  for (j=0; j < n; j++) {
    target.push(source);
  }
}

function ReplaceArrayItem (array, index, item) {
// array.splice(index, n-delete,insert-item1,.....,itemX)
  array.splice (index, 1, item);
}

function InsertArrayItem (array, index, item) {
// Insert item after index, pushing the rest 'upwards'
  array.splice (index, 0, item);
}
function ExtractArray (target, source, index, n) {
// extract n items starting at index in source giving new array target
// Create local extract and use copy method
  var lArray = source.splice(index, n);
  var j, n = lArray.length;
  target.length = 0;
  for (j=0; j < n; j++) {
    target.push(lArray);
  }
}

Thanks for listening and forcing me to think myself!

1 reply

K.Daube
Community Expert
K.DaubeCommunity ExpertAuthor
Community Expert
July 5, 2016

Can please someone enlighten me, why this works:

var arrayOfObjects = [];             // global Array

UpdateA (arrayOfObjects);            // array _is_ updated
$.writeln ("Global: arraOfObjects = " + arrayOfObjects);

function UpdateA (gArray, someotherStuff) {
var locObject = new Object();
    locObject.Name = "A";
    locObject.Item = "Something";

    for (var j= 0; j < 3; j++) {
      gArray.push(locObject);
    }
$.writeln ("Inside func: gArray = " + gArray);
}

And this does not work?

function UpdateB (gArray, someotherStuff) {
var locObject = new Object();
    locObject.Name = "A";
    locObject.Item = "Something";

var localArray = [locObject, locObject, locObject];
    gArray = localArray.slice();
$.writeln ("Inside func: gArray = " + gArray);
}

In both functions gArray receives stuff by a method - So what is the difference?

K.Daube
Community Expert
K.DaubeCommunity ExpertAuthorCorrect answer
Community Expert
July 6, 2016

Dear friends,

After much fiddling around I have found solutions to the desired array functions. One essential hint was that

gArray = [];

wihin a function creates a new array, not emptying the original one.

So Now I have a set of functions for arrays. It may be trivial for You, but not for me...

gArray = [];
gArray2 = ["1", "2", "3", "4", "5"];                               

AmendArray (gArray, "one", "two", "three", "four", "five");          //1 gArray =                
$.writeln ("1 gArray = \n   " + gArray);                             //  one,two,three,four,five  OK

DeleteArrayItem (gArray, 1);                       // remove item 2  //2 gArray =                
$.writeln ("2 gArray = \n   " + gArray);                             //  one,three,four,five      OK  

CopyArray (gArray, gArray2);                                         //3 gArray =                
$.writeln ("3 gArray = \n   " + gArray);                             //  1,2,3,4,5                OK

InsertArrayItem (gArray, 1, "two");                                  //4 gArray =                
$.writeln ("4 gArray = \n   " + gArray);                             //  1,two,2,3,4,5            OK

ReplaceArrayItem (gArray, 3, "three");                               //5 gArray =                
$.writeln ("5 gArray = \n   " + gArray);                             //  1,two,2,three,4,5        OK

ExtractArray (gArray, gArray2, 2, 3);          // extract items 3..4 //6 gArray, gArray2          OK 
$.writeln ("6 gArray, gArray2 = \n   " + gArray + "\n   " + gArray2);//  3,4,5
                                                                     //  1,2                    
function AmendArray (array, items) {
  var j, n = arguments.length;
  for (j = 1;  j < n; j++) {
    array.push(arguments);
  }
}

function DeleteArrayItem (array, index) {
// array.splice(index, n-delete,insert-item1,.....,itemX)
  array.splice (index, 1);
}

function CopyArray (target, source) {
// http://www.2ality.com/2012/12/clear-array.html tells:
// target = [];        Creates extra garbage: existing array not reused, a new one is created.
// target.length = 0;  JavaScript semantics dictate that all elements must be deleted; costs time
  var j, n = source.length;
  target.length = 0;      
  for (j=0; j < n; j++) {
    target.push(source);
  }
}

function ReplaceArrayItem (array, index, item) {
// array.splice(index, n-delete,insert-item1,.....,itemX)
  array.splice (index, 1, item);
}

function InsertArrayItem (array, index, item) {
// Insert item after index, pushing the rest 'upwards'
  array.splice (index, 0, item);
}
function ExtractArray (target, source, index, n) {
// extract n items starting at index in source giving new array target
// Create local extract and use copy method
  var lArray = source.splice(index, n);
  var j, n = lArray.length;
  target.length = 0;
  for (j=0; j < n; j++) {
    target.push(lArray);
  }
}

Thanks for listening and forcing me to think myself!