• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
1

New array functions in ColdFusion (2021 release)

Adobe Employee ,
Dec 21, 2020 Dec 21, 2020

Copy link to clipboard

Copied

Adobe ColdFusion (2021 release) includes a few new array functions. In this post, I shall list (pun unintended!) the new functions and also include the CFFiddle links so that you can also try out the functions before productionizing them.

ArrayPush

Adds an element or an object to the end of an array. For more information, see ArrayPush.

Syntax

ArrayPush(array,value)

Example 1

<cfscript>
    arr=[23,65,187,81,9]
    ArrayPush(array=arr,value=17)
    WriteOutput("The size of the array is now: " & arrayLen(arr))
</cfscript>

Try in CFFiddle

Example 2

<cfscript>
    arr=[{"id":101,"name":"John"},
         {"id":102,"name":"Paul"},
         {"id":103,"name":"George"}
        ]
    ArrayPush(arr,{"id":104,"name":"Ringo"})
    WriteOutput("The size of the array is now: " & arrayLen(arr))
</cfscript>

Try in CFFiddle

ArrayPop

Removes the last element from an array. For more information, see ArrayPop.

Syntax

ArrayPop(array)

Example

<cfscript>
    arr=[{"id":101,"name":"John"},
         {"id":102,"name":"Paul"},
         {"id":103,"name":"George"}
        ]
    // Array push
    ArrayPush(arr,{"id":104,"name":"Ringo"})
    // Array pop
    WriteDump(ArrayPop(arr))
    writeDump(arr)
</cfscript>

Try in CFFiddle

ArrayShift

Removes the first element of an array and returns the element that is removed. For more information, see ArrayShift.

Syntax

ArrayShift(array)

Example

<cfscript>
    arr=[{"id":101,"name":"John"},
         {"id":102,"name":"Paul"},
         {"id":103,"name":"George"}
        ]
    shifted=ArrayShift(arr)
    WriteDump(shifted)
</cfscript>

Try in CFFiddle

ArrayUnshift

Adds one or more elements to the beginning of an array and returns the new length of the array. For more information, see ArrayUnshift.

Syntax

ArrayUnshift(array,object)

Example 1

<cfscript>
    arr=["Mar","Apr","May","Jun","Jul"]
    // List as first element
    unshifted=ArrayUnshift(arr,"Jan,Feb") // Returns 6
    WriteOutput(unshifted)
</cfscript>

Try in CFFiddle

Example 2

<cfscript>
    arr=["Mar","Apr","May","Jun","Jul"]
    // Array as first element
    unshifted=ArrayUnshift(arr,["Jan,Feb"])
    WriteOutput(unshifted) // Returns 6
</cfscript>

Try in CFFiddle

ArrayReduceRight

Iterates over every entry of the array and calls the closure to work on the elements of the array.  For more information, see ArrayReduceRight.

Syntax

ArrayReduceRight(array, function(result, item, [,index, array])[, initialValue])

Example 1

<cfscript>
       data = ['1','2','3','4','5','6'];
       stringConcat = ArrayReduceRight(data,function(previous,next) {
        return previous & next;
       },"");
       writeOutput(stringConcat)
</cfscript>

Try in CFFiddle

Example 2

<cfscript>
    data=[3,5,7,9,11]
    result=ArrayReduceRight(data,function(previous,next){
        return previous & next
    },"")
    writeDump(result)
</cfscript>

Try in CFFiddle

TOPICS
Getting started

Views

518

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Dec 22, 2020 Dec 22, 2020

Copy link to clipboard

Copied

Hi, Saurav! How is ArrayPush different from ArrayAppend? I know we all like to have "push" and "pop" functionality with arrays in general, but it seems like we have two names for the same kind of function now.

 

Dave Watts, Eidolon LLC

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Dec 23, 2020 Dec 23, 2020

Copy link to clipboard

Copied

Dave Watts : I know we all like to have "push" and "pop"

 

Funny you should say that, Dave. I think it's because there is a familiar swing to the pushin' and the poppin'.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Dec 26, 2020 Dec 26, 2020

Copy link to clipboard

Copied

LATEST

There is, in any case, one difference between arrayAppend and arrayPush. The one returns a boolean (whether or not the item was successfully appended), the other, an integer (the number of items in the resulting array).

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources
Documentation