Article
· May 1, 2020 1m read

Code sample to concatenate JSON arrays

ObjectScript doesn't include any built-in method for appending one JSON dynamic array to another. Here's a code snippet I use that's 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.

ClassMethod ConcatArrays(pArgs...) As %DynamicArray
{
    set outArray = ##class(%DynamicArray).%New()
    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
}

Feel free to let me know if there's a better way to do this!

Discussion (2)2
Log in or sign up to continue