Hi,
I would like to copy a Dataset, not make a reference to the same dataset
newDataset=oldDataset will not do because this will create a reference not a copy
any ideas ??
Regards,
Hi,
I would like to copy a Dataset, not make a reference to the same dataset
newDataset=oldDataset will not do because this will create a reference not a copy
any ideas ??
Regards,
Need to do a deep copy for arrays, objects and datasets. A couple of code samples:
/*
* TITLE : CODE_copy_dataset
*
* MODULE : rsrc_CODE_frameworks
*
* ABOUT : creates a copy of a dataset (not a reference), and returns it
*
* INPUT : dataset
*
* OUTPUT : copy of dataset
*
* REQUIRES :
*
* MODIFIED : Jul 2008 -- David Workman, Data Mosaic
*
*/
var original = arguments[0]
var columns = new Array()
for ( var i = 1 ; i <= original.getMaxColumnIndex() ; i++ ) {
columns[i - 1] = original.getColumnName(i)
}
var deepCopyObj = databaseManager.createEmptyDataSet(0, columns)
for ( var i = 1 ; i <= original.getMaxRowIndex() ; i++ ) {
deepCopyObj.addRow([i], columns.length)
var row = original.getRowAsArray(i)
for ( var j = 1 ; j <= row.length ; j++ ) {
deepCopyObj.setValue(i, j, original.getValue(i, j))
}
}
return deepCopyObj
/*
* TITLE : CODE_copy_object
*
* MODULE : rsrc_CODE_frameworks
*
* ABOUT : creates a copy of an object (not a reference), and returns it
*
* INPUT : object
*
* OUTPUT : copy of object
*
* REQUIRES :
*
* MODIFIED : Mar 2008 -- Troy Elliott, Data Mosaic
*
*/
var origObj = arguments[0]
//if passed an array, create an array
if (origObj instanceof Array) {
var deepCopyObj = new Array()
}
//if passed an object, create an object
else {
var deepCopyObj = new Object()
}
for (var i in origObj) {
if (typeof origObj[i] == 'object' && origObj[i] != null) {
deepCopyObj[i] = globals.CODE_copy_object(origObj[i])
}
else {
deepCopyObj[i] = origObj[i]
}
}
return deepCopyObj
Hi David,
Thanks for the code samples.
I hoped there might be a simpler way to do this, but this will do the trick.
Regards,
Hi,
B.t.w for an Array there is an other option :
var oldArray = ["a", "b", "c"];
var newArray = oldArray.slice(); // makes an independent copy of oldArray
The array.slice() is a nice shortcut. One note – it doesn’t duplicate array properties. So if you do stuff like the following, you’ll need to use the custom deep copy method to get an accurate copy:
var x = new Array("a","b","c")
x.a = "apple"
x.b = "beer"
x.c = "can"
var xCopy = x.slice()
application.output(xCopy[xCopy[0]])
//output is null
var xDeepCopy = globals.CODE_copy_object(x)
application.output(xDeepCopy[xDeepCopy[0]])
//output is "apple"
There’s also this trick…
var clone = eval(obj.toSource());
Note also that the “slice()” solution for arrays will copy objects in the array by reference, I’m pretty sure.
greg.
Hi Greg,
Thanks !!
B.t.w. the slice() solution does not copy by reference, see example below
oldArray = ["a", "b", "c"];
var newArray = oldArray.slice();
oldArray[1]='xx'
newArray[1]='yy'
application.output(oldArray[0] + ' - ' + newArray[0])
application.output(oldArray[1] + ' - ' + newArray[1])
application.output(oldArray[2] + ' - ' + newArray[2])
the result is :
a - a
xx - yy
c - c
agiletortoise:
There’s also this trick…var clone = eval(obj.toSource());…
greg.
Great trick. Something new in 4.0.