Created
May 4, 2020 14:00
-
-
Save isc-rsingh/18f63b05abf7cdaff31b414d860f2b44 to your computer and use it in GitHub Desktop.
Concatenate JSON arrays in ObjectScript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// 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