Skip to content

Instantly share code, notes, and snippets.

@isc-rsingh
Created May 4, 2020 14:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save isc-rsingh/18f63b05abf7cdaff31b414d860f2b44 to your computer and use it in GitHub Desktop.
Save isc-rsingh/18f63b05abf7cdaff31b414d860f2b44 to your computer and use it in GitHub Desktop.
Concatenate JSON arrays in ObjectScript
/// ObjectScript does not include any built-in method for appending one JSON dynamic array to another.
/// This is equivalent to the JavaScript concat() method.
/// Call it with any number of arguments to concatenate them into a new array.
/// If an argument is a dynamic array, its elements will be added. Otherwise the argument itself will be added.
/// Thanks to Pravin Barton in this article https://community.intersystems.com/post/code-sample-concatenate-json-arrays
ClassMethod ConcatArrays(pArgs...) As %DynamicArray
{
set outArray = ##class(%DynamicArray).%New() // or []
for i=1:1:pArgs {
set arg = pArgs(i)
if ($IsObject(arg) && arg.%IsA("%DynamicArray")) {
set iter = arg.%GetIterator()
while iter.%GetNext(.key, .value) {
do outArray.%Push(value)
}
} else {
do outArray.%Push(arg)
}
}
return outArray
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment